$result is actually an array which looks like this:
Array ( [book_title] => Bioethics in the 21st Century [id] => 1424
[isbn] => 978-953-307-270-8 [unix_name] =>
bioethics-in-the-21st-century [visible_online] => 1 )
This is my view(better said...poor attempt of a view ). I'm trying to get an alignment based on the index of the array. Like so:
http://pastebin.com/z13PZWe8
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header"
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?=$result;?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thank you for your help!
Are you trying to do this?
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header">
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?php echo $result['book_title']; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['isbn']; ?></td>
<td><?php echo ($result['visible_online']) ? 'Yes' : 'No'; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As a side note, <?=$var;?> syntax should be avoided since short_open_tag is disabled in many PHP installations, and this was required to use that syntax until PHP 5.4.0
Depends on how You got the result form database it will be something like this:
<td><?=$result['book_title']?> </td>
<td><?=$result['id']?> </td>
<td><?=$result['isbn']?> </td>
<td><?=$result['visible_online']?> </td>
Or if You're using doctrine:
<td><?=$result->book_title?> </td>
<td><?=$result->id?> </td>
<td><?=$result->isbn?> </td>
<td><?=$result->visible_online?> </td>
You should read tutorial, things like that are in it :)
http://framework.zend.com/manual/en/zend.db.statement.html
...
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<?php foreach($result as $cell)?>
<td><?=$cell;?> </td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
...
Like this ?
Related
I am new to php and I couldn't find the answer on stack. I am experimenting and trying to figure out how I can use the superglobal GET to display an image and also change the *count depending on how the user modifies it in the URL.
I would like to change the Beach image count times.
So the URL would look something like:
website.com/table?beach_image=2&count=4
and would display the beach image #2, 4 times:
bathing suit
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$count = 1;
$beach_image = filter_input(INPUT_GET,"beach_image");
?>
for my html:
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td>0</td>
<td>Sunglasses</td>
</tr>
<tr>
<td>1</td>
<td>Sunscreen</td>
</tr>
<tr>
<td>2</td>
<td>Bathing Suit</td>
</tr>
<tr>
<td>3</td>
<td>Hat</td>
</tr>
</table>
<div>
<img src="images/<?= $beach[$beach_image]?>">
</div>
You can do it like below
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$keysArr = array_fill(0,count($beach),0);//array_combine(array_keys($beach),array_fill(0,count($beach),0));
$beach_image = filter_input(INPUT_GET,"beach_image");
$count = filter_input(INPUT_GET,"count");
$keysArr[$beach_image] = $count;
?>
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td><?php echo $keysArr[0] ;?></td>
<td>Sunglasses</td>
</tr>
<tr>
<td><?php echo $keysArr[1] ;?></td>
<td>Sunscreen</td>
</tr>
<tr>
<td><?php echo $keysArr[2] ;?></td>
<td>Bathing Suit</td>
</tr>
<tr>
<td><?php echo $keysArr[3] ;?></td>
<td>Hat</td>
</tr>
</table>
<div>
<?php for($i=1;$i=$count;$i++){?>
<img src="images/<?= $beach[$beach_image]?>">
<?php }?>
</div>
I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.
I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.
I want to add a class to every last <tr> of a main category. I don't want to add the classname to every <tr>. What can I change in my script?
Like this:
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
<th></th>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
</table>
<table class="data forum">
<? foreach ($this->mainCategories as $mainCategory): ?>
<tr>
<th><strong><?= $this->escape($mainCategory->fcName) ?></strong></th>
<th> </th>
<th><strong>Topics</strong></th>
<th><strong>Laatste topic</strong></th>
</tr>
<? foreach ($mainCategory->getSubCategories() as $category): ?>
<tr>
<td><?= $this->escape($category->getName()) ?></td>
<td><?= $this->escape($category->fcDescription) ?></td>
<? if ($this->escape($category->numTopics) > 0): ?>
<td><?= $this->escape($category->numTopics) ?></td>
<td><?= date_create($this->escape($category->last_topic))->format('d-m-Y H:i') ?></td>
<? else: ?>
<td> </td>
<td> </td>
<? endif ?>
</tr>
<? endforeach ?>
<tr>
<td class="split"></td>
</tr>
<? endforeach ?>
</table>
Grab the count of your $this->mainCategories variable and test for the last one against a counter variable...
$count = count($this->mainCategories;
$current = 0;
<?
foreach ($this->mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
Update:
Let's look at a simplified version as a proof of concept:
<?php
$mainCategories = array("Eins", "Zwei", "Drei");
$count = count($mainCategories);
$current = 0;
foreach ($mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
<td><?= $mainCategory ?></td>
</tr>
<?php endforeach ?>
This produces the following HTML:
<tr>
<td>Eins</td>
</tr>
<tr>
<td>Zwei</td>
</tr>
<tr class="someClass">
<td>Drei</td>
</tr>