How to pull in an array to a PHP MySQL output? - php

I am trying to pull in this date for each part of my database, but I am missing some element to make it work and not sure what I am missing?
$modified[] = array(date("m/d/y", strtotime($row[audit_modify_date]))."<br />".date("g:i a", strtotime($row[audit_modify_date])),);
//query
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified</td>";
echo"<tr>";
}
They output just says Array.

You are printing the array, not the elements in the array
echo "<td>$modified</td>";
either
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>".$modified[$i]."</td>";
echo"<tr>";
}
or
foreach ($modified as $m) {
echo $m;
}
edit actually what? I don't understand what you're doing

Since you are using $modified[ ] = array(....), it becomes multidimensional array.
Use below code
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified[0][$i]</td>";
echo"<tr>";
}
Or just
echo "<td>$modified[0][0]</td>";
Other thing is instead of creating a array, make it a variable like shown below
$modified=date("m/d/y", strtotime($row[audit_modify_date]))."<br />".date("g:i a", strtotime($row[audit_modify_date]));
//query
for ($i=0; $i < count($result); $i++) {
echo "<tr>";
echo "<td>$modified</td>";
echo"<tr>";
}
Note: use the appropriate according to your requirement.

Related

two dimension array with assoc and for loop php

I write that code but i got error
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
echo "<table border=1>";
for($i = 0; $i <count($tablica); ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$i]); $x++){
echo "<td>".$tablica[$i][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";
it's simple but when i add assoc for 1st dimension it's broke with that error
Notice: Undefined offset: 0 in C:\wamp64\www\projects\xd\1.php on line 10
Warning: count(): Parameter must be an array or an object that implements Countable in C:\wamp64\www\projects\xd\1.php on line 10
You are adding complexity when there is no need to.
When using associative arrays, foreach is usually a best way to iterate.
You can replace your code with:
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd'=>['jabłska', 'wisssśnie', 'grussssszki']
];
echo "<table border=1>";
foreach($tablica as $tr) {
echo "<tr>";
foreach($tr as $td) {
echo "<td>".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
PS: you have duplicated key named xd. Then PHP will only get the last one.
If you want to stick with using for() loops, then the problem is that your array has xd and xd1 as the keys, but your trying to access them with $i which is 0 and 1. What you can do is create an array of the keys (I've set $keys using array_keys()) and then index that using $keys[$i]...
$tablica = [
'xd'=>['jabłka', 'wiśnie', 'gruszki'],
'xd1'=>['jabłska', 'wisssśnie', 'grussssszki']
];
$n=count($tablica);
$keys = array_keys($tablica);
echo "<table border=1>";
for($i = 0; $i < $n; ++$i) {
echo "<tr>";
for($x = 0; $x < count($tablica[$keys[$i]]); $x++){
echo "<td>".$tablica[$keys[$i]][$x]."</td><br>";
}
echo "</tr>";
}
echo "</table>";

Loop through a PHP Multidimensional Array

Im trying to create a 2D array with a specific number of rows and cols which I have stored as the variable $n ; so if $n was 5, I would have 5 rows and 5 cols, all with random numbers. I have created a for loop (as shown below) that generates the correct amount of rows but I cannot figure out how to do the same with the columns at the same time. The code I have at the moment is shown below.
<?php
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
for($j=0; $j<$n; $j++) { // Runs the loop times $n
print "<tr>";
for($k=0; $k<$n; $k++) { // Runs the loop times $n
print "<td>" . $value[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
?>
Any help would be appreciated in learning to create this loop of the array. Thanks in advance.
You need a second loop inside the first loop:
for($i=0; $i<=$n; $i++) { // rows
for($j=0; $j<=$n; $j++) { // columns
$value[$i][$j] = rand(1,20);
}
}
Try this instead :
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
foreach($value as $row){
print "<tr>";
foreach($row as $cell){
print "<td>" . $cell . "</td>";
}
print "</tr>";
}
print "</table>";
And using a for loop :
print "<table>";
for($i=0; $i<=$n; $i++) { // rows
print "<tr>";
for($j=0; $j<=(count($value[$i])-1); $j++) { // columns
echo "<td>".$value[$i][$j]."</td>";
}
print "</tr>";
}
print "</table>";

PHP Array, Get every 4 results and output in a loop

I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)

php array with in array names don't output

Is this possible to do?
i'm looking to build a grid of values, but I was wondering if this is possible
$section_array = array(
$array_1[$i],
$array_2[$i],
$array_3[$i]
);
for ($i = 1; $i <= 31; $i++) {
echo "<tr>";
for ($j = 0; $j < 3; $j++) {
echo "<td>" . $section_array[$j] . "</td>"; // 3
}
echo "</tr>";
}
This is basically the idea, I want to print out 1 value of each section (array_1, array_2) etc
then move to the next day, and print out the values of day 2 and so on. is it possible without having to just list every single one in the 2nd for loop?
Are you looking for something like this? Loop through your array of arrays and print 2 values from the sub array.
$arr = //insert data here
foreach($arr as $subarr) {
echo '<tr><td>' . $subarr['myval1'] . '</td><td>' . $subarr['myval2'] . '</td></tr>';
}
$secname_array = array($val1, $val2, $val3);
$daycount = 31;
for ($i = 1; $i <= $daycount; $i++){
echo"<tr>
<td>$i</td>";
foreach ($secname_array as $v)
{
echo "<td>". $v[$j]. "</td>";
}
$j++;
echo"</tr>";
}
?>

PHP loop in object

I have 4 instances of the same object:
echo $payment->field_course_1_length[0]['value'];
echo $payment->field_course_2_length[0]['value'];
echo $payment->field_course_3_length[0]['value'];
echo $payment->field_course_4_length[0]['value'];
My question is so that I don't have to be typing the same over and over can I put it in a loop like this:
for ($i=1; $i<5; $i++) {
echo $payment->field_course_'$i'_length[0]['value'];
}
Thank you.
echo $payment->{"field_course_{$i}_length"}[0]['value'];
$tmp = $payment->{'field_course_' . $i . '_length'};
echo $tmp[0]['value'];
However, I strongly recommend to use arrays instead of dynamic properties. If they are not dynamic, don't access them dynamic.
You can do like this:
for($i = 1; $i <= 4; $i++) {
echo $payment->{"field_course_".$i."_length"}[0]['value'];
}

Categories