I have been writing a code snippet of PHP in HTML which iterates an array inside another array (data from mysql). But when I run the code, it gives the following two errors.
Undefined variable
Array to string conversion in ......
Following is my code.
$sql = "SELECT * FROM person";
$result = mysqli_query($conn, $sql);
$resultDataSet = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
array_push($resultDataSet, $row);
}
if ($type == "HTML") {
$htmlSerialize = "
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<?php foreach($resultDataSet as $value): ?>
<tr>
<th><?php=?".$value['Name']." ?></th>
<th><?php=?".$value['Age']." ?></th>
<th><?php=?".$value['City']." ?></th>
</tr>
<?php endforeach; ?>
</table>";
echo $htmlSerialize;
}
Also following are the errors.
What is the error I have made? How can I solve this?
Edited
Following is the var dump of $resultDataSet
array (size=2)
0 =>
array (size=8)
0 => string '1' (length=1)
'ID' => string '1' (length=1)
1 => string 'Akila' (length=5)
'Name' => string 'Akila' (length=5)
2 => string '22' (length=2)
'Age' => string '22' (length=2)
3 => string 'Mount Lavinia' (length=13)
'City' => string 'Mount Lavinia' (length=13)
1 =>
array (size=8)
0 => string '2' (length=1)
'ID' => string '2' (length=1)
1 => string 'Randil' (length=6)
'Name' => string 'Randil' (length=6)
2 => string '23' (length=2)
'Age' => string '23' (length=2)
3 => string 'Colombo' (length=7)
'City' => string 'Colombo' (length=7)
The error reports that value is undefined. PHP does not parse nor compile code wrapped by quotes -- strings. Try this where I split the loop from the HTML output.
if ($type == "HTML") {
// Start by opening the table and header
$htmlSerialize = '
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>';
// loop over the results and append each row to $htmlSerialize
foreach($resultDataSet as $value):
$htmlSerialize .= '
<tr>
<th>'. $value['Name'] .'</th>
<th>'. $value['Age'] .'</th>
<th>'. $value['City'] .'</th>
</tr>';
endforeach;
// close the table
$htmlSerialize .= '</table>';
// flush results
echo $htmlSerialize;
}
I get this array from a mysql query:
array (size=9)
0 =>
array (size=2)
'room_category' => string 'MALE GENERAL WARD' (length=17)
'vacant_beds' => string 'MG-8,MG-2,MG-4,MG-6,MG-7' (length=24)
1 =>
array (size=2)
'room_category' => string 'FEMALE GENERAL WARD' (length=19)
'vacant_beds' => string 'FG-4,FG-1,FG-2,FG-3' (length=19)
2 =>
array (size=2)
'room_category' => string 'MOTHER CHILD WARD' (length=17)
'vacant_beds' => string 'MC-2,MC-4,MC-5,MC-6' (length=19)
3 =>
array (size=2)
'room_category' => string 'TWIN' (length=4)
'vacant_beds' => string 'TW-A1,TW-A2,TW-B2,TW-C1,TW-C2' (length=29)
4 =>
array (size=2)
'room_category' => string 'NICU' (length=4)
'vacant_beds' => string 'NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5' (length=48)
5 =>
array (size=2)
'room_category' => string 'CLASSIC' (length=7)
'vacant_beds' => string 'CL-6,CL-8,CL-4,CL-5' (length=19)
6 =>
array (size=2)
'room_category' => string 'DELUXE' (length=6)
'vacant_beds' => string 'DLX-5,DLX-6' (length=11)
7 =>
array (size=2)
'room_category' => string 'EXECUTIVE' (length=9)
'vacant_beds' => null
8 =>
array (size=2)
'room_category' => string 'AC GENERAL WARD' (length=15)
'vacant_beds' => string 'AG-5,AG-1,AG-2,AG-3,AG-4' (length=24)
Now I want to display this array in html table.
I have tried this, where I can achieve it only partly.
I want the string part that is string 'MG-8,MG-2,MG-4,MG-6,MG-7' to be in separate columns.
echo "<table>";
foreach($rows as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
from which I get table like below
MALE GENERAL WARD MG-8,MG-2,MG-4,MG-6,MG-7
FEMALE GENERAL WARD FG-4,FG-1,FG-2,FG-3
MOTHER CHILD WARD MC-2,MC-4,MC-5,MC-6
TWIN TW-A1,TW-A2,TW-B2,TW-C1,TW-C2
NICU NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5
CLASSIC CL-6,CL-8,CL-4,CL-5
DELUXE DLX-5,DLX-6
EXECUTIVE
AC GENERAL WARD AG-5,AG-1,AG-2,AG-3,AG-4
Every item of your comma-separated string can be received by exploding string by ,. But as every string can have different number of elements - first you need to find the value of max elements. Thus you need to iterate $rows twice - first find max count of elements and second - echo tds. So we can do this:
echo "<table>";
$new_rows = [];
$max_count = 0;
foreach ($rows as $key => $row) {
$elements = [];
if (!empty($row['vacant_beds'])) {
$elements = explode(',', $row['vacant_beds']);
if (sizeof($elements) > $max_count) {
$max_count = sizeof($elements);
}
}
$new_rows[] = [
'name' => $row['room_category'],
'elements' => $elements,
];
}
foreach ($new_rows as $row) {
echo '<tr>';
// echo name
echo '<td>' . $row['name'] . '</td>';
// main part - items in elements we will wrap into `td`:
foreach ($row['elements'] as $e) {
echo '<td>' . $e . '</td>';
}
// if number of elements in `$row['elements']` is less
// than `$max_count` - we should add empty `<td>`
if (sizeof($row['elements']) < $max_count) {
echo str_repeat('<td></td>', $max_count - sizeof($row['elements']));
}
echo '</tr>';
}
echo "</table>";
Handle the needed key(in your case - vacant_beds) for further processing:
echo "<table border='1'>";
foreach($rows as $row) {
echo "<tr>";
foreach($row as $key => $row2){
echo "<td>" .
(($key == 'vacant_beds')? implode("</td><td>", explode(",", $row2)) : $row2)
. "</td>";
}
echo "</tr>";
}
echo "</table>";
i want to echo the array value with the key of type , here is my code,
foreach ($sam as $key => $sa) {
foreach ($sa as $s) {
echo $s['type'];
}
}
and this is the content of array $sam
array (size=1)
0 =>
array (size=5)
'type' => string 'days' (length=4)
'bookable' => string 'no' (length=2)
'priority' => int 10
'from' => string '1' (length=1)
'to' => string '1' (length=1)
my serier of foreach code results Warning: Illegal string offset 'type' and i dont know why? can you help me echo type thank you.
You only need one foreach:
foreach ($sam as $s) {
echo $s['type'];
}
I try to make a table html, with 2 table PHP,
I don't manage to put my value which is in my 4th row, 3th column in the 4th row, 4th column.
My PHP/html:
<table BORDER=1px>
<tr>
<?php
foreach($tableau1 as $value)
{
echo '<th>'.$value.'</th>';
}
?>
</tr>
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
foreach($valu as $vava => $keke)
{
if($val==$vava){echo '<td>'.$keke.'</td>';}
}
}
echo '</tr>';
}
?>
</table>
My PHP table (first:table , second:table1)
array (size=3)
6 =>
array (size=2)
'marque' => string 'marque 6' (length=8)
'modele' => string 'modele 6' (length=8)
3 =>
array (size=4)
'marque' => string 'marque 3' (length=8)
'modele' => string 'modele 3' (length=8)
2 => string 'bois art3' (length=9)
4 => string 'beton art3' (length=10)
5 =>
array (size=3)
'marque' => string '-lepetit' (length=8)
'modele' => string 'modele 5' (length=8)
4 => string 'beton art5' (length=10)
array (size=4)
'marque' => string 'marque' (length=6)
'modele' => string 'modèle' (length=6)
2 => string 'bois' (length=4)
4 => string 'beton' (length=5)
Response to first answer:
:) I already tried it but it's not good I get too much cell in all row and I don't understand why ...
I think you need to print an empty <td></td> when no data for the cell.
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
$found = false;
foreach($valu as $vava => $keke)
{
//echo '<td>' . $val . ' ' . $vava . ' ' . $keke . '</td>';
if($val==$vava){
echo '<td>'.$keke.'</td>';
$found = true;
}
}
if (!$found) { echo '<td></td>'; }
}
echo '</tr>';
}
?>
Added a boolean to flag when data was added to the column and then moved the
echo '<td></td>';
outside of the inner loop.
var_dump:
array (size=2)
0 =>
array (size=16)
'campaignID' => string '416' (length=3)
'trackerID' => string '401' (length=3)
'bannerID' => string '9901193' (length=7)
'CookieID' => string '7ed9340d8f971e1a396afc3e01fb3ab7' (length=32)
'affiliateid' => int 1782
'ConversionConnectionID' => string '6712988' (length=7)
'conversionTime' => string '2014-06-14 09:09:18' (length=19)
'conversionStatus' => string 'Pending' (length=7)
'userIp' => string '27.0.57.16' (length=10)
'actionkey' => string 'gte500' (length=6)
'window' => int 20410
'variables' =>
array (size=9)
'cartvalue' => string '752.53' (length=6)
'clickid' => string '' (length=0)
'conversionid' => string '' (length=0)
'event' => string '' (length=0)
'leadid' => string '' (length=0)
'optionaladver' => string 'producttype,saleamount,modeofpayment' (length=36)
'quantity' => string '' (length=0)
'timestamp' => string '1970-01-01 00:00:00' (length=19)
'transactionid' => string '1107063NJP517A934' (length=17)
'sub_conversion' => int 0
'updated_date' => string '0000-00-00 00:00:00' (length=19)
'sum_affiliate_commission' => float 125
'type' => string 'Sale' (length=4)
'TotalRows' => int 1
I am going to print the data from this array using following code:
<?php foreach ($results as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement['campaignID']; ?></td>
<td><?php echo $licenseElement['trackerID']; ?></td>
<td><?php echo $licenseElement['conversionTime']; ?></td>
<td><?php echo $licenseElement['userIp']; ?></td>
</tr>
<?php endforeach; ?>
But it gives me an error of Undefined index: for all columns.
What's wrong with my code?
You are dealing with a multidimensional array, and your foreach is looping through the first dimension - which does not contain the indices specified. Try this:
foreach ($results[0] as $licenseElement) { ...
You need to loop through the array stored as the first element in the array $results, not $results itself. Thus $results[0] contains the array you want to loop through, with all the correct indices.
Try:
<?php
$x = 0;
while (isset($results[$x])):
$licenseElement = $results[$x];
?>
<tr>
<td><?php echo $licenseElement['campaignID']; ?></td>
<td><?php echo $licenseElement['trackerID']; ?></td>
<td><?php echo $licenseElement['conversionTime']; ?></td>
<td><?php echo $licenseElement['userIp']; ?></td>
</tr>
<?php
$x++;
endwhile;
?>