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.
Related
I have a text file:
[{"title": "...", "Book": ["A", "B"]}]
How can I read it and put into table using php??
Do something like this.
if you need to read a text file then use file_get_contents() function.
<?php
$fh = file_get_contents('text.txt');
$table = json_decode($fh);
?>
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
</tr>
</thead>
<tbody>
<?php foreach($table as $val){ ?>
<tr>
<td><?php echo $val->title; ?></td>
<td><?php echo $val->Book[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
NOTE Set your table head as per your need.
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 was watching a video tutorial on how to create a basic CMS with PHP and database but I'm wondering why the reason to open the <?php tag two times.
Can't I just use a single PHP block?
<?php
include("includes/db.php");
if(isset($_GET['view_page'])){ //open curly brace which will close
//later..what???
?>
<table width="1000" border="2px" align="center">
<tr>
<td style="text-align:center;background-color:yellow"colspan='6'><h2>All pages here</h2></td>
</tr>
<tr>
<th>Page No.</th>
<th>Page Title</th>
<th>Page Content</th>
<th>Delete</th>
</tr>
<tr>
<?php
$query="SELECT * FROM `pages`";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$p_id =$row['p_id'];
$p_title=$row[1];
$p_desc =substr($row[2],0,100); //on table show 0 to 100 characters long
?>
<td><?php echo $p_id; ?></td>
<td><?php echo $p_title; ?></td>
<td><?php echo $p_desc; ?></td>
<td>Delete</td>
</tr>
<?php }} ?> //THIS IS REASON OF CONFUSION
</table>
This is the beauty of php it can be inserted any where in html tag's not in html page . Page must be saved with .php extension to write the php code you have to just write
<?php
#code
?>
let say if you want to use while loop but you want some html tags in inside the loop you can easily do this by
<?php
while(#condition) {
//inside php tag code
?>
<p> i am html part depend on php codtion</p>
<?php
} //end of while loop
?>
there are other ways to
<p>
<?php
echo"hello p tag i am from php ";
?>
</p>
Try like this
<?php
include("includes/db.php");
if(isset($_GET['view_page'])){ //open curly brace which will close
//later..what???
?>
<table width="1000" border="2px" align="center">
<tr>
<td style="text-align:center;background-color:yellow"colspan='6'><h2>All pages here</h2></td>
</tr>
<tr>
<th>Page No.</th>
<th>Page Title</th>
<th>Page Content</th>
<th>Delete</th>
</tr>
<tr>
<?php
$query="SELECT * FROM `pages`";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$p_id =$row['p_id'];
$p_title=$row[1];
$p_desc =substr($row[2],0,100); //on table show 0 to 100 characters long
echo '<td>'. $p_id; .'</td>';
echo '<td>'. $p_title .'</td>';
echo '<td>' .$p_desc .'</td>';
echo '<td>Delete</td>';
}
?>
</tr>
</table>
<?php } ?>
the problem is that you have added one addition } and additionally you have closed <tr> inside the loop that should be done outside... Like this
<td>Delete</td>
<?php } ?>
</tr>
the second } that is of if(isset(...)) shpuld be closed after the <table> has been closed
$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 ?
I'm trying to use this chart generator from http://htmldrive.net/items/show/792/Rare-Accessible-charts-using-jQuery-and-HTML5.html
Here's the code which loads data from mysql database:
The query works, but I guess my interpretation of the example provided in the site was wrong.
I get an output if I do it this way(predefined data):
<tr>
<th scope="row">Profit</th>
<td>5</td>
<td>5</td>
</tr>
But when I do it this way I get a blank output:
?>
<table>
<caption> Reports</caption>
<thead>
<tr>
<td></td>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<th scope="col"><?php echo $row['Cust_Name']; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Subtotal</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTAL_PUR']; ?></td>
<?php } ?>
</tr>
<tr>
<th scope="row">Profit</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTALPROFIT']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
Here's what I'm getting:
After the first iteration through the rows, when you display the customer names, the fetch data pointer is at the end of the dataset... you're trying to fetch the set again without resetting the pointer.
Try issuing
mysql_data_seek($query, 0);
before the while loops to display total and profit