I have some code which takes my results from the database and then wraps them in to a div, so that each of the results are situated in to individual boxes.
Whilst the results are fine, the problem i am having is that the boxes are repeating inside one another and not formating in to individual boxes which list below one another.
below is my php code
$sVisit_Id='';
while ($row = mysql_fetch_array ($result)) {
$url1 = $row['url1'];
if ($row['visit_id']!=$sVisit_Id)
{
echo '</table>';
?> <div class="box1095"><?php
echo '<table><tr><td class="test" width="350px;"><strong>'.$referrer.'</strong><br /></td><td>'.$search_term.'</td></tr><table>';
$sVisit_Id=$row['visit_id'];
echo '<tr bgcolor="#333" height=30px"><td width="200px">Date/Time</td><td width="750px">Webpage</td><td width="150px"></td></tr>';
}
echo '<tr class="active" bgcolor="" onMouseOver="this.bgColor="gold";" onMouseOut="this.bg"#222222";"><td width="200px" border="">'.$row['timedate'].
'</td><td width="750px" border="">'. $row['url1'].'</td><td width="15px" align="right" border="">'. "<a href=". $url1 ." class=''> " ?><img src="images/go_button.fw.png" width="100" height="30" alt="GO" target="_blank"/><?php "</a>" .'</td></tr>';
} ?></div><?php
echo '</table>';
The image below shows a screenshot of the results.
Any suggestions or guidance would be appreciated.
Thanks
You should try something like this
echo '<table>';
while ($row = mysql_fetch_array ($result)) {
// everything goes here
echo '<tr><td>Webpage</td><td></td></tr>';
echo '<tr><td>some text</td><td>some text</td><td><img src="" /></td></tr>';
}
echo '</table>';
Keep table out of the loop (both, starting and closing tags), so, there will be one table with multiple rows (tr).
Update : You have (<table>...<table>) between if condition, as given below
echo '<table><tr><td class="test" width="350px;"><strong>'.$referrer.'</strong><br /></td><td>'.$search_term.'</td></tr><table>';
There is no closing tag, instead you have <table> so, your table is never closed. Also, there is only echo '</table>'; at the beginning of if condition, without starting tag (within loop) but closing tag is outside of loop.
Update : Also you have following (pointed out by David) at the end of your last line within the loop, (not echoed/printed).
<?php "</a>" .'</td></tr>';
Related
I want to create a PHP page to display the results of a query in a MySQL database under the format of a table. By spending quite some time on different forums, I ended with something that is somehow satisfying me but that is strongly affecting the design and the layout of my webpage. Due to the fact that I wrote the code by a test-fail strategy, it is far from being straightforward and I am sure it is possible to shorten and simplify it and, therefore, make it more compatible with the format of my webpage. Could anybody have a look at it and give some suggestions of general interest about how to solve this kind of issues?
<div id="main">
<?php
require_once('../mysqli_connect.php');
$response = $db->query("SELECT * FROM metabolite");
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">
<tr><td align="center"><b>Metabolites</b></td>
<td align="center"><b>KEGG Id</b></td>
<td align="center"><b>Synonyms</b></td></tr>';
while ($data = $response->fetch())
{
?>
<tr><td align="left">
<?php echo $data['Metabolite_name']; ?></td>
<td align="left">
KEGG: <?php echo $data['Synonyms']; ?></td>
<td align="left">
<?php echo $data['Synonyms']; ?></td>
</tr>
<?php
}
$response->closeCursor();
?>
</div>
I thank you in advance for all your effort and your help.
Tom.
There's no way we can improve the design and layout of your webpage with the code you've given us. What I can do is write 'better' readable code.
<?php
function tableCell($content)
{
echo '<td align="left">'.$content.'</td>';
}
// database access
require_once('../mysqli_connect.php');
// get all records from the metabolite table
$response = $db->query("SELECT * FROM metabolite");
// start main division
echo '<div id="main">';
// start the table
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">';
// walk through all the metabolite records
while ($data = $response->fetch())
{
// start a row
echo '<tr>';
// create the cells
tableCell($data['Metabolite_name']);
tableCell('KEGG: '.$data['Synonyms']);
tableCell($data['Synonyms']);
// finish a row
echo '</tr>';
}
// close the table
echo '</table>';
// close main division
echo '</div>';
// close query
$response->closeCursor();
But this is not worth much, the output should remain the same.
if ($response->num_rows > 0) {
while($data = $response->fetch_assoc()) {
echo "<tr><td>" . $data["Metabolite_name"]. "</td></tr>" . ;
}
}
else {
echo "0 results";
}
I am trying to get some div tags inside my php. The original code that is working looks like this:
echo "Date: ".$row{'date'}."<br>"."Day: ".$row{'day'}."<br>"."From Time: ".$row{'fromtime'}."<br>"."To Time: ".$row{'totime'}; //display the results
When I run this code, the data is gonna be below each other. I would like that each output from the database, is coming out in a row and in some div tags, so I can control it. I have tried to make it, but I am really in doubt how the syntax are?
<?php
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
echo "<div class="column2">""Day: ".$row{'day'}"</div>";
echo "<div class="column3">""From Time: ".$row{'fromtime'}"</div>";
echo "<div class="column4">""To Time": ".$row{'totime'}"</div>";
?>
This dosent work. Does anybody know how I can solve this?
New code:
Thanks a lot for both your answers. I am actually thinking of putting in a table instead. When I print out the div, it looks like this:
http://postimg.org/image/jq60jujv1/
So I guess it would be something with
//fetch the data from the database
Print"<h3>Return from database:</h3>"."<br>";
print '<table border="1">';
print '<tr>';
print '<td>date</td>';
print '<td>day</td>';
print '<td>fromtime</td>';
print '<td>totime</td>';
print '</tr>'."<br>";
print '</table>'."<br>";
while ($row = mysql_fetch_array($result)) {
print "<tr>"."<td>"."Date: ".$row{'date'}."</td>"."</tr>";
print "<tr>"."<td>"."Day: ".$row{'day'}."</td>"."</tr>";
print "<td>"."From Time: ".$row{'fromtime'}."</td>";
print "<td>"."To Time: ".$row{'totime'}."</td>"."<br>";
}
or?
Best Regards
Mads
try this instead of yours,
<?php
echo "<div class='column1'>"."Date: ".$row{'date'}."</div>";
echo "<div class='column2'>"."Day: ".$row{'day'}."</div>";
echo "<div class='column3'>"."From Time: ".$row{'fromtime'}."</div>";
echo "<div class='column4'>"."To Time: ".$row{'totime'}."</div>";
?>
Because of the wrapping quotation marks you use for echo ".." you can not use the same quotation marks inside that string:
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
^--- string ended
You can solve this in several ways, using single quotation marks inside is already suggested, but you can also escape the ones you are currently using:
echo "<div class=\"column1\">Date: ".$row{'date'}."</div>";
^ ^
You can avoid all these problems by not using echo for output of all markup, but rather start/end php tags the place you actually want to print
variables or do some logic:
<div class="column1">Date: <?php echo $row{'date'}; ?></div>
Output as table
A HTML table use the <table> and </table> tags,
<tr> is a table row, <th> for table
headers and <td> for regular table cells.
<?php
/* the code block where you connect to db etc.. */
?>
<table>
<!-- headers -->
<tr>
<th>Date</th>
<th>Day</th>
<th>From</th>
<th>To</th>
</tr>
<!-- Now a row for each new set of data, here you probably need to
loop through some data set you retrieve from the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row{'date'};?></td>
<td><?php echo $row{'day'};?></td>
<td><?php echo $row{'fromtime'};?></td>
<td><?php echo $row{'totime'};?></td>
</tr>
<?php endwhile; ?>
</table>
Try this
<?php
echo "<div class='column1'>Date: ".$row{'date'}."</div>
<div class='column2'>Day: ".$row{'day'}."</div>
<div class='column3'>From Time: ".$row{'fromtime'}."</div>
<div class='column4'>To Time: ".$row{'totime'}."</div>";
?>
Edit:
If you want output as table
<table>
<tr>
<th>Date</th>
<th>Day</th>
<th>From Time</th>
<th>To Time</th>
</tr>
<?php
foreach($result as $row){
echo "<tr><td>".$row{'date'}."</td>
<td>".$row{'day'}."</td>
<td>".$row{'fromtime'}."</td>
<td>".$row{'totime'}."</td></tr>";
}
?>
</table>
I have in my database a table "Categories" where I have this:
And now with php Im showing the categories without "father", in other words the categories when the id_father is null. Like Warnings and Tecnology.
I´m doing like this:
<div class="block cat" style="display:block">
<div class="title">Categories:</div>
<?php
$readCat = $pdo->prepare("SELECT * FROM categories where id_father IS null");
$readCat->execute();
$num_rows_readCat = $readCat->rowCount();
$readCategoriesResult = $readCat->fetch(PDO::FETCH_ASSOC);
if(!$num_rows_readCat >= 1)
{
echo 'There is no categories registers yet.';
}
else
{
?>
<table width="560">
<tr class="cts">
<td>category Name:</td>
<td>Content</td>
<td>tags:</td>
<td>Date:</td>
</tr>
<?php
foreach($readCategoriesResult as $cat)
echo '<tr>';
echo '<td>'.$cat['name'].'</td>';
echo '<td>'.$cat['content'].'</td>';
echo '<td align="center"><img src="ico/ok.png" alt="3 Tags" title="3 Tags" /></td>';
echo '<td align="center">'.date('d/m/Y H:i',strtotime($cat['date'])).'</td>';
echo '</tr>';
And its working fine, I already have a my first category "Warnings" showing.
But I only have the first category and I want to show every category that I have in my database, in this case I also have Technology but its only showing the Warnings category.
I alraedy tried with a foreach loop but its not working...
Somebody there is seeing where is the problem?
while ($row = $readCat->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['content'].'</td>';
echo '<td align="center"><img src="ico/ok.png" alt="3 Tags" title="3 Tags" /></td>';
echo '<td align="center">'.date('d/m/Y H:i',strtotime($row['date'])).'</td>';
echo '</tr>';
}
The while loop means all rows will returned will be echoed out.
$readCat->fetch(PDO::FETCH_ASSOC); will only fetch one result. You need to either:
Use $readCat->fetchAll(PDO::FETCH_ASSOC); and use a foreach loop, as mentioned.
Use $readCat->fetch(PDO::FETCH_ASSOC); in a while loop.
Here's the while loop:
while ($result = $readCat->fetch(PDO::FETCH_ASSOC)){
//Echo HTML here
}
If you want to get all the records, you will need to do a fetchAll then loop over the records:
$readCategoriesResult = $readCat->fetchAll(PDO::FETCH_ASSOC);
...
foreach($readCategoriesResult as $cat){
//display the table row
}
Alternatively, you could fetch in a while loop, but I like fetchAll
Somebody there is seeing where is the problem?
The problem is lack of foreach loop and may be some confusion between fetch methods. you can read on them in the tag wiki
I am trying to make a table via PHP, but when I load this, it displays it like this..
The code:
<table border="1" cellpadding="5">
<?php
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
?>
</table>
My goal is to display a table of 4 columns by 3 rows..
EDIT:
Found it already, it makes a new <tr> tag everytime the first while is performed.
The block :
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
will create 3 cells 1x, 1x, 2x first,
next the block:
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
will create cells from Monitor, so the result become like this.
To fix it, you should save the result from the first while loop to an array and go through at the 2nd while loop
You are echoing nested rows.
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';// Here you open a row
echo $test["count"];
echo 'x</td>';//No closing of row, you close the td and open another tr on the next iteration
};
I need to generate a table with php, that will display the images - names stored on database. It has to display 3 images in a row. The images are added to the database all the time, so I need that to be automatically generated, instead of hard coding the tables. I am not sure how do I do that? Please help!
You need to cycle the result recordset and print out the new row every 3rd element.
For example:
<table>
<tr>
<?php $i=0; foreach ($images as $image): ?>
<td><?php echo $image['name'] ?> <img src="<?php echo $image['path'] ?>" /></td>
<?php if(++$i%3==0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>
suppose u get the all images name from database in an array
$img_array = array(
1=>'f.jpg',
2=>'s.jpg',
3=>'t.jpg',
4=>'f.jpg',
5=>'e.jpg'
);
// now create dynamic table via php
<table border="1" cellpadding="2" cellspacing="2" width="100%">
<tr>
<?php
$i=0;
foreach($img_array as $k){
if($i%3==0) { ?> </tr><tr> <?php } ?>
<td><img src="<?php echo $k?>" border="0"></td>
<?php $i++; } ?>
</tr>
</table>
Note: please write full path of image in src before <?php echo $k?>
Iterate the image records, using modulo 3 to change to the next table row.
Something like:
echo '<table><tr>';
foreach ($images) {
echo '<td>$image</td>';
if ($i % 3 == 0) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
A simple table like that would be like
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
To generate this automatically you need to store where you are in the table, first col, 2nd col or 3th col.
<?php
$pos = 1;
print "<table>"
for ($i=0; $i<=10;$i++)
{
if ($pos==1)
{
print "<tr><td>1</td>";
$pos=2;
}
else if ($pos==2)
{
print "<td>2</td>";
$pos=3;
}
else if ($pos==3)
{
print "<td>3</td></tr>";
$pos=1;
}
}
if ($pos==2 || $pos==3)
print "</tr>";
print "</table>"
Keep in mind that if you use the options with $i%3 from the other comments, that your table will start end/or finish with an empty row. This would need additional checks. The $i%3 will give the remainder of the division of $i and 3. So when $i/3 == 0, means it is true on every third row.