SQL into html cells and rows - php

I have some code that shows results in a table
Right now it will show 3 cells per row. The issue is that the first row shows 2 then creates a new row with the expected behavior fine after that. How can I get the first row to show 3 cells
if(++$cnt == 0) {
echo '<tr>';
} elseif($cnt % 3 == 0) {
echo '</tr><tr>';
}
echo "<td bgcolor='#009933'><a href='" . $row['local_link'] . ".m4v' name='video'>" . $row['local_link'] . "</a></td>";
echo "<td><video width='320' height='240' controls='controls'>
<source src='" . $row['local_link'] . ".m4v" . "' type='video/mp4' />
Your browser does not support the video tag.
</video></td>";
}
}
echo "</table>";

You're preincrementing $cnt on your first line, meaning it will never be zero - unless you started it at -1. Try changing your first line to:
if($cnt++ == 0) {
echo '<tr>';
} // etc
This way the value of $cnt is incremented after you compare it to zero, not before.

Related

PHP displays only one word from MySQL record in URL variable

A PHP page displays only one word from a MySQL record in a URL variable, while it displays the correct 2 words in the HTML output, I tried many solutions such '".$row['book_category']."' and {$row['book_category']} etc. but I still get the same one result.
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "" . $row['book_category'] . "" . "</td>";
echo "</tr>";
}
?>
So now first $row insert is only one word in the url, while the second $row outputs two words properly as expected; the problem is I need the two words to be passed as variable in the URL.
URLs cannot have a space character, this is why you are seeing the first word only, you need to encode the value received using rawurlencode().
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "<a href='sidebar_cat_display.php?book_cat=" . rawurlencode($row['book_category']) . "'>" . $row['book_category'] . "</a>" . "</td>";
echo "</tr>";
}
?>
Keep in mind you will also need to adjust the sidebar_cat_display.php page to fetch the book_cat parameter from the URL accordingly if you aren't already.

Why does only one of these .php functions work

I'm currently trying to put php results into an HTML table, and not sure why it is not working. The first block returns the data I need, so I know it's not a sql query issue. The second block (where I try to make it a table) returns the headers but no data.
Here is my code:
<h1>
<?php
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print odbc_result($weekadmissions, "date") . ", " .
odbc_result($weekadmissions, "attendance") .
"<br />\n";
}
?>
</h1>
<?php
print "<table><tr>";
print "<th>Date</th>";
print "<th>Attendance</th></tr>";
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print "<tr><td>" . odbc_result($weekadmissions,"date") . "</td>";
print "<td>" . odbc_result($weekadmissions, "attendance") . "</td></tr>";
}
print "</table>";
?>
Thanks!
The answer was that the result was consumed by the first loop - thank you FirstOne for the solution

How can I use hyperlinks in PHP to display data on the page it sends you to?

I have this piece of code below. It displays the image and name of all the entries in a table in my database. The name is set up to become a hyperlink.Is it possible to make it so when one specific name is clicked that data for only that specific name will be displayed on the page you are sent to?
So for example if I select the first entry that is displayed back "mealname1" and it takes me to the showrecipe.php page, can I make it so I can display all the data I have for "mealname1" and only "mealname1". I'm really lost, I have scoured the internet and my php books but can't find anything to that is relevant.
If there is no way of doing it is there an obvious solution that I am missing?... I am very much a novice to this... thanks for your help guys.
<?php
require("db.php");
$prodcatsql = "SELECT * FROM recipes";
$prodcatres = mysql_query($prodcatsql);
$numrows = mysql_num_rows($prodcatres);
if($numrows == 0)
{
echo "<h1>No Products</h1>";
echo "There are no recipes available right now.";
}
else
{
echo "<table id='recipetable'>";
while($prodrow = mysql_fetch_assoc($prodcatres))
{
echo "<tr>";
if(empty($prodrow['image'])){
echo "<td><img
src='./images/No_image.png' alt='"
. $prodrow['mealname'] . "'></td>";
}
else {
echo "<td><img src='./images/".$prodrow['image']
. "' alt='"
. $prodrow['mealname'] . "'></td>";
}
echo "<td>";
echo ''.$prodrow['mealname'].'';
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Change the query to
SELECT * FROM recipes WHERE mealname='$mealname' LIMIT 1;
You can remove "LIMIT 1" if you want but this makes sure you will only get 1 or 0 row back. Don't forget to escape the string.

Printing table with div to create delete section

The code I am posting below, iterates through a result set and prints elements (paths to images on database) so that images display in order. Only 3 images per row. I want to be able to have the same concept (i.e. 3 images or cells per row) but I want to have another div underneath it to have the word delete photo.
$n = 3;
echo "<table style='margin-right: 100px;'>";
echo "<tr>";
for($i=1; $i<=count($gallery);$i++){
$temp = array();
$temp = $gallery[$i-1];
echo "<td><div id='gallery_pic'><img id='single_pic' src='". $temp->path . "' /></div></td>";
if($i % $n ==0){
echo "</tr><tr>";
}
}
echo '</tr>';
echo '</table>';
echo "</table>";
The idea is that owner of profile should be able to delete photo by clicking it. I will handle that I am just not sure how to handle printing the table with same order by adding another row per row with delete word.
Simple. Add a new div right after div#gallery_pic and give it a width of 100%, this will force it to be below the image...
echo "<table style='margin-right: 100px;'>";
echo "<tr>";
for($i=1; $i<=count($gallery);$i++){
$temp = array();
$temp = $gallery[$i-1];
echo "<td><div id='gallery_pic'><img id='single_pic' src='". $temp->path . "' /></div><div class='delete_wrap'><a href='?delete_id=" . {image_id} . " style='width:100%'>Delete</a></div></td>";
if($i % $n ==0){
echo "</tr><tr>";
}
}
echo '</tr>';
echo '</table>';
echo "</table>";

SQL Image Database assembling rows into HTML cells and rows

I have a database of images which I want to output as a gallery ...
I want to display each image in its own cell <td>image1</td> and limit the number of cells to 7 per row.
<tr>
<td>image1</td><td>image2</td><td>image3</td><td>image4</td><td>image5</td><td>image6</td><td>image7</td>
</tr>
Then a new row would be created and continue to output all the images.
<tr>
<td>image8</td>
and so on ..
I know how to do a query, but I am lost as to how to assemble the rows into the format I am looking for.
Can anyone please help me it would be greatly appreciated. Thanks.
First run your query, then get the mysql_fetch_assoc in a variable. Then echo your beginning tags for the table and tr. Then create a foreach loop of the query assoc, as another variable such as row, and echo $row['image'] in a td. This will add a new td with the image for every entry in the database. Then echo your closing tags for the tr and table.
$query = mysql_query("SELECT image FROM table_name");
$query_a = mysql_fetch_assoc($query);
echo "<table><tr>"
foreach ($query_a as $row)) {
echo "
<td>" . $row['image'] . "</td>
";
}
echo "</tr></table>";
Not tested, but try something like this
<table>
<?php
// make sure there is at least 1 row first
$cnt = 0;
$while($row = mysql_fetch_assoc($query))
{
if(++$cnt == 1) {
echo '<tr>';
} elseif($cnt % 7 == 0) {
echo '</tr><tr>';
}
// show images in <td>'s
echo "<td>" . $row['image']. "</td>";
}
echo '</tr>';
?>
</table>
Keep it simple:
Query the database to get the desired information, loop through the results, construct a new row come each new loop.
$sql = "Select * from table";
mysql_query($sql) or die(mysql_error());
if(mysql_num_rows !=0)
{
// Found at least one record, create table
echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($sql)
{
$cell_count = 0;
// Create a new row for each record found
echo "<td>" . $row['image_column']. "</td>";
$cell_count++;
if($cell_count == 7)
{
echo "</tr>";
echo "<tr>";
$cell_count = 0;
}
}
// Close the table
echo "</tr>";
echo "</table>";
}

Categories