Cakephp make child array editable - php

My model is a user which can have multiple Email addresses. I can view them by:
<?php foreach ($user->email_addresses as $addresses): ?>
<tr>
<td><?= h($addresses->id) ?></td>
<td><?= h($addresses->email) ?></td>
</tr>
?>
I try to make them editable as input but is does not work:
<?php foreach ($user->email_addresses as $addresses): ?>
<tr>
<td><?= $this->Form->control('addresses.id'); ?></td>
<td><?= $this->Form->control('addresses.email'); ?></td>
</tr>
?>

<?php foreach ($user->email_addresses as $index=>$addresses): ?>
<tr>
<td>
<?= $this->Form->control('email_addresses.'.$index.'.id') ?>
<?= $this->Form->control('email_addresses.'.$index.'.email') ?>

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>

php pdo json encode array

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>

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>

Multiple SELECT variables are not displaying

Having issues displaying all of the selected data entries inside SELECT. Right now the only one that is displaying is the recipe_direct data. Each table data should list in one row the name, ingred, direct, auth name and auth email.
<div class="starter-template">
<h1>Recipes</h1>
</div>
<?php
try
{
$sql = 'SELECT recipe_id, recipe_name, recipe_ingred, recipe_direct, author_name, author_email FROM recipes';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching recipes: ' . $e->getMessage();
}
while ($row = $result->fetch())
{
$recipes[$row['recipe_id']] = $row;
}
?>
<p>Add a Recipe</p>
<?php foreach ($recipes as $id => $recipe): ?>
<blockquote>
<table class="table table-striped">
<tr>
<td><?php echo $recipe['recipe_name']; ?></td>
<td><?php echo $recipe['recipe_ingred']; ?></td>
<td><?php echo $recipe['recipe_direct']; ?></td>
<td><?php echo $recipe['author_name']; ?></td>
<td><?php echo $recipe['author_email']; ?></td>
</tr>
<?php htmlout($recipe_html); ?> |
edit |
delete
</table>
</blockquote>
<?php endforeach; ?>
You overwrite $recipe_html each time you assign a value to it
why not just echo out the table rows:
<table>
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?= $recipe['recipe_name']; ?></td>
<td><?= $recipe['recipe_ingred']; ?></td>
<td><?= $recipe['recipe_direct']; ?></td>
<td><?= $recipe['author_name']; ?></td>
<td><?= $recipe['author_email']; ?></td>
</tr>
<?php endforeach; ?>
</table>
EdIt as #Fred -ii- states, the table tags should be outside the loop
Everything looks correct up until your foreach.
<blockquote>
<table class="table table-striped">
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?php $recipe_html = $recipe['recipe_name']; ?></td>
<td><?php $recipe_html = $recipe['recipe_ingred']; ?></td>
<td><?php $recipe_html = $recipe['recipe_direct']; ?></td>
<td><?php $recipe['author_name']; ?></td>
<td><?php $recipe['author_email']; ?></td>
</tr>
<p> //seems you were missing opening <p> tag//
<?php htmlout($recipe_html); ?> |
edit | //can use shorthand here//
delete //can use shorthand here//
</p>
<?php endforeach; ?>
</table>
</blockquote>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

Categories