CodeIgniter Dynamic rows from JavaScript table entries - javascript

I am trying to grab my post data from my view and have it there on page reload (after it passes through validation), I am really struggling with this as I am only just learning JavaScript so anything dynamic to my is completely new...
What the data looks like in a print_R():
Array ( [firstname] => [lastname] => [address1] => [address2] => [addressSuburb] => [addressState] => [addressPostcode] => [email] => [addressCountry] => AU [orderRef] => [orderNote] => [orderTrack] => [counter0] => 0 [itemCode0] => 1234 [Desc0] => Test1 [Qty0] => 1 [Cost0] => 5 [counter1] => 1 [itemCode1] => 5678 [Desc1] => Test 2 [Qty1] => 2 [Cost1] => 10 [create] => Create Order )
What my current view loops look like:
<?php if($this->input->post()):?>
<?php foreach($this->input->post('counter') as $counter): ?>
<tr>
<?php foreach($this->input->post() + $counter AS $key => $value): ?>
<td>
<input type="text" name="<?php echo $key + $counter ?>" value="<?php echo $value ?>"/>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endif; ?>
The Error I am getting:
Severity: Warning
Message: Invalid argument supplied for foreach()
I assume its because counter isnt actually an array, just part of it... I dont know how to output each line of entry data into its own array, then it would actually be rather easy to do...
Please don't give me a hard time for having it all in the view, as I am learning heaps easier to have it centralized there while trying to get an output.

Related

How can i show the recently added bookmarked posts on the top of list using php or other tools?

I am working on a website that gives users an option to bookmark or favorite any post to read it later on. If anyone favorites a post then it is added to that list.
The list is showing the more recent posts (according to the post's published date) on the top but I want the list to show in order that the posts were favorited. This is because when a user favorites any old post then they have to struggle through a bundle of their favorited posts to get to the recently bookmarked/favorited post
<?php
$favorites = get_user_favorites();
$x = 1;
?>
<?php
if ($favorites) : $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$favorites_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'post__in' => $favorites,
'paged' => $paged
));
echo "<ol class='favorite-list-ol'>";
?>
<?php
if ($favorites_query->have_posts()) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
?>
<li class="favorite-list-li">
<?php echo get_the_title($favorite); ?>
</li>
<?php
endwhile;
$x++;
echo "</ol>";
?>

Running JS-File in php

I’ve a questionnaire that I analyze with php. The data are supposed to be displayed in a Java-based polar-graph and a table.
You can see how it looks with static data here.
However, the graph doesn’t show when I use the data from my database. The table works fine. A few days ago I asked about how to integrate php-data to java via json_encode because I thought this might be the problem, but I learned (thanks again) that this is the correct approach.
My web-hoster says that it doesn’t support php in html, so I modified the index.html to an index.php. I have no idea what to modify to make it work. Database-connection works, php-values in the table are correct, the js-graph works fine with static data.
Here are my php-arrays:
$arr1 = array(axis => "Gesundheitszustand", value => $X1P);
$arr2 = array(axis => "BMI", value => $X3);
$arr3 = array(axis => "Stress", value => $X10P);
$arr4 = array(axis => "Körperliche Aktivität", value => $X4P);
$arr5 = array(axis => "Nahrung: Gemüse/Obst", value => $X8d);
$arr6 = array(axis => "Nahrung: Fisch", value => $X8f);
$arr7 = array(axis => "Nahrung: Fleisch", value => $X8h);
$arr8 = array(axis => "Geistige Gesundheit", value => $X9P);
$arr9 = array(axis => "Zufriedenheit", value => $X2P);
$arr10 = array(axis => "Rauchen", value => $X9a);
The values are numbers between 0 and 1.
That’s how I try to include the js-file in my php-file:
<html>
<head>
<script src="d3js.js"></script>
<script src="radarchart.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php ?>
<script type="text/javascript" src="script.js"></script>
<?php ?>
</body>
And that’s how I include my php-data to the js-file:
var d = [
[
<?php echo json_encode($arr1); ?>,
<?php echo json_encode($arr2); ?>,
<?php echo json_encode($arr3); ?>,
<?php echo json_encode($arr4); ?>,
<?php echo json_encode($arr5); ?>,
<?php echo json_encode($arr6); ?>,
<?php echo json_encode($arr7); ?>,
<?php echo json_encode($arr8); ?>,
<?php echo json_encode($arr9); ?>,
<?php echo json_encode($arr10); ?>,
],[
{axis:"Gesundheitszustand",value:0.63},
{axis:"BMI",value:0.58},
{axis:"Stress",value:0.67},
{axis:"Körperliche Aktivität",value:0.33},
{axis:"Nahrung: Gemüse/Obst",value:0.66},
{axis:"Nahrung: Fisch",value:0.25},
{axis:"Nahrung: Fleisch",value:0.50},
{axis:"Geistige Gesundheit",value:0.68},
{axis:"Zufriedenheit",value:0.7},
{axis:"Rauchen",value:0.91},
]
];
Although json_encode is the correct way, it has to be the data-integration, correct? Or am I missing something?
Any suggestions? Many thanks for your comments/help in advance!
Use echo to output a string directly

Using php, how should I get the upcoming date from an array and pass it to javascript?

I have setup a loop for WordPress which gets the date from a custom field (date picker) and passes it to a array. I need to determine the closest date in the future (upcoming date) from that array.
<?php
$today = current_time('m/d/Y');
$args=array(
'meta_key' => 'opening',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'post_type' => 'event',
);
$query = new WP_Query($args);
?>
<?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$date = DateTime::createFromFormat('Ymd', get_field('opening'));
$dates[] = $date->format('m/d/Y');
endwhile; endif; wp_reset_postdata();
print_r($dates);
?>
And pass it to javascript.
$('#tl').timeline({
startItem : '$resultOfUpcomingDateEvaluation',
});
Please advice, thanks for the time.
Found a solution, little dirty so will be nice if someone has a more elegant one.
I wrote a second loop with a similar setup, limiting the posts to 1 and comparing the meta_value with a var that gets current_time. Then i echo the result wrapping it in a div with a class that hides it (not elegant).
<?php
$today = current_time('Ymd');
$args=array(
'meta_key' => 'opening',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 1,
'post_type' => 'event',
'meta_query' => array(
'key' => 'opening',
'compare' => '>=',
'value' => $today,
)
);
$query = new WP_Query($args); ?>
<?php if($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$date = DateTime::createFromFormat('Ymd', get_field('opening'));
$upcoming = $date->format('d/m/Y');
endwhile; endif; wp_reset_postdata();
echo '<div class="hide" id="upcomingdiv">';
echo $upcoming;
echo '</div>';
?>
After that, on my javascript file, i get the element by ID and get the .textContent of the div that contains the result of the loop and store it to a variable.
var setupUpcoming = document.getElementById("upcomingRef");
var upcoming = setupUpcoming.textContent;
Finally i pass the variable to the startItem.
$('#tl').timeline({
startItem : upcoming,
});

How to load different textboxes based on url and database data?

I'm trying to control the loading of different textboxes according to the url of the page and prior settings in the database.
For example: I have a database table that looks like this:
And I'd like my page to look like that when browsing www.mysite.com/us/mypage
like that when browsing www.mysite.com/canada/mypage
like that when browsing www.mysite.com/italy/mypage
So I'm trying to understand how to design my code. Should it be addressed only on the client side, with javascript on the page load or should it be handled with the controller on the server side.
Thanks!
First off, since you already have the rules. Set it up first. Second, you need to parse the url (get the country and treat it like a slug) and feed it into the rules. Third, then just use a normal foreach loop and a condition inside (1/0 or true/false) if it needs to be printed or not. Consider this example:
<?php
// $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = 'www.mysite.com/us/mypage';
$url2 = 'www.mysite.com/canada/mypage';
$url3 = 'www.mysite.com/italy/mypage';
// dummy values
// setup the rules
$rules = array(
'us' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 1,
'textbox4' => 1,
),
'canada' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 0,
'textbox4' => 0,
),
'italy' => array(
'textbox1' => 1,
'textbox2' => 0,
'textbox3' => 1,
'textbox4' => 0,
),
);
// properly parse the url
$current_url = $url2; // i just chosen canada for this example
if (!preg_match("~^(?:f|ht)tps?://~i", $current_url)) {
$current_url = "http://" . $current_url;
}
$current_url = array_filter(explode('/', parse_url($current_url, PHP_URL_PATH)));
$country = reset($current_url);
?>
<!-- after getting the slug/country, loop it with a condition -->
<form method="POST" action="">
<?php foreach($rules[$country] as $key => $value): ?>
<?php if($value == 1): ?>
<label><?php echo $key; ?></label>
<input type="text" name="<?php echo $key; ?>" /><br/>
<?php endif; ?>
<?php endforeach; ?>
<input type="submit" name="submit" />
</form>
<!-- textbox1 and textbox3 should be the only inputs in here since i picked canada -->
You have to set condition and check the value
if($isset($textbox) && $textbox==1)
{
//print the label and text box
}
Best thing is try to achive the output at server side.
I think the following code will be useful for you.
Try to get the country code from url and get the database values into $textboxA array, and run the following code.
$textboxA = array(1,1,0,1);
foreach($textboxA as $key => $value){
switch($key){
case 0: if($value) print $textbox1;
break;
case 1: if($value) print $textbox2;
break;
case 2: if($value) print $textbox3;
break;
case 3: if($value) print $textbox4;
break;
default: print "";
}
}

CakePHP ajax form submit before and complete will not work for displaying animated gif

I"m using prototype I've tested ALL possibble scenarios, and my busy indicator WILL not show no matter what. I've cross browser tested no luck.
<?php
echo $ajax->submit('Submit', array('url'=> array('controller'=>'records', 'action'=>'add'), 'update' => 'ajax_div',
'evalScripts' => true,
'before' => $this->Js->get('#busy-indicator')->effect('show', array('buffer' => false)),
'complete' => $this->Js->get('#busy-indicator')->effect('hide', array('buffer' => false))));
?>
<?php echo $this->Html->image('ajax-loader.gif', array('id'=>'busy-indicator')); ?>
Well. I finally got this working. The manual had no indication of this method but it works like a charm for prototype:
<?php
echo $ajax->submit('Submit',
array('url'=> array('controller'=>'records',
'action'=>'add'), 'update' => 'ajax_div',
'evalScripts' => true,
'loading' => 'Element.show(\'busy-indicator\')',
'success' => 'Element.hide(\'busy-indicator\')'));
?>

Categories