php pdo json encode array - php

hello I want to print "title" in the menu content data in the database, but I get an error. I do not know much about json, but as a result of my research, something like this came out and the error code is below
Warning: Illegal string offset 'title' in menu.php on line 59
mysql database data
menu_content
[{"title":"HelloWord","address":"HelloAdres","phone":"HelloPhone","submenu":[{"email":"HelloSubmenuEmail","phone":"HelloSubmenuPhone","fax":"HelloSubmenuFax"}]}]
pdo
$query = $db->prepare('SELECT * FROM menu ORDER BY menu_id DESC');
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
table
<tbody>
<?php foreach ($rows as $row): ?>
<tr data-id="<?= $row['menu_id'] ?>">
<td width="90"><?= $row['menu_id'] ?></td>
<td width="90"><?= $row['menu_title'] ?></td>
<td><?php json_decode($row['menu_content']['title'],true) ?></td>
<td><?= $row['menu_date'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>

You should convert the JSON column before attempting to make use of its parts.
<tbody>
<?php
foreach ($rows as $row):
$menu = json_decode($row['menu_content']);
?>
<tr data-id="<?= $row['menu_id'] ?>">
<td width="90"><?= $row['menu_id'] ?></td>
<td width="90"><?= $row['menu_title'] ?></td>
<td><?= $menu[0]->title ?></td>
<td><?= $row['menu_date'] ?></td>
</tr>
<?php
endforeach;
?>
</tbody>

Try is code :
<tbody>
<?php foreach ($rows as $row)
{
$menu_content=json_decode($row['menu_content'],true)
?>
<tr data-id="<?= $row['menu_id'] ?>">
<td width="90"><?= $row['menu_id'] ?></td>
<td width="90"><?= $row['menu_title'] ?></td>
<td><?= $menu_content[0]['title'];?></td>
<td><?= $row['menu_date'] ?></td>
</tr>
<?php
}
?>
</tbody>

Related

How to print out two queries in one table?

I have two queries in a form. I print out the results like this:
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to present each result in a different column. But the result from the second query ("Column4") seems to be presented in a second table (??). It's not echoed next to the other columns, but below:
Current Output
How can I fix this issue?
You have to change
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
into
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
You have begun another row ('tr' tag closed and re-opened).
Try this fix...
I have removed the things that are causing it to echo on next line
Now this will work...
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

codeigniter looping data in foreach using for

i want to looping data in foreach using for in html so i don't want to type <input type> one by one.
Edit :
Sorry I did not inform you completely,so in my database i have columns like this.
//my database
team_id
//Participants 1
name_1
phone_1
email_1
//Participants 2
name_2
phone_2
email_2
//Participants 3
name_3
phone_3
email_3
//view
//$data is from my controller
<?php foreach($data as $rowdata) {
//1
$name_1=$rowdata->name_1;
$phone_1=$rowdata->phone_1;
$email_1=$rowdata->email_;
//2
$name_2=$rowdata->name_2;
$phone_2=$rowdata->phone_2;
$email_2=$rowdata->email_2;
//3
$name_3=$rowdata->name_3;
$phone_3=$rowdata->phone_3;
$email_3=$rowdata->email_3;
}?>
<?php for($i=1;$i<=3;$i++){ ?>
<tr>
<td>Name</td>
<td><?php echo $name_$i ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo $phone_$i ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $email_$i ?></td>
</tr>
so, how i can looping like that using for,Thanks
In your comments, you answered that $data is the result from your db.
I assume that your db has 3 columns Name, Phone and Email and NOT Name_1, Name_2, Phone_1 etc...
Code:
<?php foreach($data as $rowdata) { ?>
<tr>
<td>Name</td>
<td><?= $rowdata['name'] ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata['phone'] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata['email'] ?></td>
</tr>
<?php } ?>
$data is an array of objects, so don't need to use 2 loops for getting and print data.You can do this in one loop.
<table>
<?php foreach($data as $rowdata) { ?>
<tr>
<td>Name</td>
<td><?= $rowdata->name ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata->phone ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata->email ?></td>
</tr>
<?php }?>
</table>
I don't know syntax is ok but logic is below
<table>
<?php
$i=1;
foreach($data as $rowdata) {
?>
<tr>
<td>Name</td>
<td><?= $rowdata->name.'_'.$i; ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata->phone.'_'.$i; ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata->email.'_'.$i; ?></td>
</tr>
<?php $i++; }?>
</table>

Display json content in php table

I am trying to display json content in php table but I'm getting error every time. I have some syntax error and cant figure out what should i change?
PS. Trying built it with Slim framework
Here is my code:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<?PHP
foreach($myObject as $key=>$item);
?>
<tr>
<td><?PHP echo $item->Date; ?></td>
<td><?PHP echo $item->Time; ?></td>
<td><?PHP echo $item->Round; ?></td>
<td><?PHP echo $item->Circuit; ?></td>
<td><?PHP echo $item->Location; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
My error is:
Notice: Undefined property: stdClass::$Date in C:\xampp\htdocs\challenge\app\view\challenge.php on line 38
Notice: Undefined property: stdClass::$Time in C:\xampp\htdocs\challenge\app\view\challenge.php on line 39
Notice: Undefined property: stdClass::$Round in C:\xampp\htdocs\challenge\app\view\challenge.php on line 40
Notice: Undefined property: stdClass::$Circuit in C:\xampp\htdocs\challenge\app\view\challenge.php on line 41
I have just looked at your json format in comparison to the code. The path mapping to the json is incorrect. I have rectified that below;
Please review the following:
PHP code:
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
For each format:
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
Full Code:
<html>
<head>
<title>PHP</title>
</head>
<body>
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
?>
<table>
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
The problem is that decoded array looks differently. Dump your encoded array before the loop to understand the right data structure or use JSON beautify service. Also use the right upper/lowercase to address properties. Updated loop may look like this:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
<?PHP
foreach($myObject->MRData->RaceTable->Races as $key=>$item){
?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitName; ?></td>
<td><?PHP echo $item->Circuit->Location->locality; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
</div>

merge two foreach in one layout

i got two arrays.
from results i want to make a table with foreach
but i dont know how to make it work in one table row..
this is what i got
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
</tr>
<?php endforeach; ?>
</table>
<table>
<?php foreach ($applications as $application) : ?>
<tr><td><?php echo $application->name; ?></td></tr>
<?php endforeach; ?>
</table>
so what i want is simply add to the first table another column with $application->name;
what i'm missing here??
thanks
<table>
<?php
$count = count($appky);
for($i=0; $i< $count; $i++) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appky[$i]->name; ?></td>
<td width="20%"><?php echo $appky[$i]->all_items;?></td>
<td width="20%"><?php echo $appky[$i]->published; ?></td>
<td width="20%"><?php echo $appky[$i]->unpublished; ?></td>
<td width="20%"><?php echo $applications[$i]->name; ?></td>
</tr>
<?php } ?>
</table>
use for instead of foreach
<table>
<?php for($i=0 ; $i<count($appky) ; $i++ ) { ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka[$i]->name; ?></td>
<td width="20%"><?php echo $appka[$i]->all_items;?></td>
<td width="20%"><?php echo $appka[$i]->published; ?></td>
<td width="20%"><?php echo $appka[$i]->unpublished; ?></td>
<td width="20%"><?php echo isset($applications[$i]) ? $applications[$i]->name : '' ; ?></td>
</tr>
<?php } ?>
</table>
Assuming your two arrays aren't necessarily in order - If you have some data that matches between your $appka and $application you can try something like this below:
<table>
<?php foreach ($appky as $appka) : ?>
<tr class="counter_apps" height="20px" >
<td width="40%"><?php echo $appka->name; ?></td>
<td width="20%"><?php echo $appka->all_items;?></td>
<td width="20%"><?php echo $appka->published; ?></td>
<td width="20%"><?php echo $appka->unpublished; ?></td>
<?php foreach ($applications as $application) : ?>
<?php if ($appka->id == $application->id) { ?>
<td><?php echo $application->name; ?></td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Otherwise you wont be able to spit out the correct application name. The above solution uses an id field, but you probably hove something else you can match on.

Categories