How can I display this table correctly? - php

I am working on a very simple webshop, it should echo different products in a table of 2 <td> by 4 <tr> but now it only displays the different products downwards. Hopefully someone here can help me.
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<table><tr><td>'.$artikel.'</td>';
echo '</table>';
}

Why would it display a table of 2 by 4?
You are putting every product in its own table, so the first thing to do, is move the table tags out of the loop and then you need to add logic to add a new row after every x products.
Although you don't seem to need a table as your div has fixed dimensions, so you can just get rid of the table and use css to get the grid you want.

If this is your entire code then you're missing a tr tag.
Could be a possible cause for your lay-out shifting.
Instead of using breaks, I'd recommend using multiple divs (or table lay-out if you're more comfortable with that) as breaks aren't always handled the same in all browsers.

Instead of creating new table for every records, you can use one row per record.
$result=mysql_query("select * from products");
if(mysql_num_rows($result) > 0){
echo '<table>';
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<tr><td>'.$artikel.'</td></tr>';
}
echo '</table>';
}

Related

Create a table with blank squares for missing mysql data

I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.

How to break the row into parts to fit the screen size

I am using PHP to display a row from MySQL table based on the user input.
Since the table has many columns, I retrieve a lot of columns, for which the user has to scroll right to see all the data.
I am using the following code to display the content to the page.
$sql2="select * from student_information where student_id='$id'";
$result2=mysqli_query($conn,$sql2);
if($result2->num_rows > 0){
echo '<table width="100%" bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" border="1" class="db-table">';
echo '<tr><th>STUDENT__FIRST_NAME</th><th>STUDENT__LAST_NAME</th><th>STUDENT_ID</th><th>ADDRESS</th><th>CITY</th><th>STATE</th><th>ZIP</th><th>COUNTRY</th><th>PHONE</th><th>DEPARTMENT</th><th>MAJOR</th>
<th>EMAIL</th><th>MAILING_ADDRESS</th><th>HEALTH_INSURANCE</th><th>CREDIT_HOURS</th><th>GROUP</th><th>GENDER</th><th>DOB</th><th>STATUS</th><th>NOTES</th><th>JOINED_ON</th></tr>';
while($rowz2 = mysqli_fetch_assoc($result2)){
echo "<tr>";
foreach($rowz2 as $key=>$value){
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo "</table><br/>";
}
else {
echo "<h2>No data based on the entered values</h2>";
}
How can I break out of the output row and into two rows, so that the output row will be displayed in two parts, so that it fits the screen, without the need to scroll right.
You can't assume the user screen has any defined width. it can range from a cell phone to an ultra wide monitor... better your table stays one record per line.
You can use some css responsive table tricks though
Check this out https://css-tricks.com/examples/ResponsiveTables/responsive.php

Printing out MySQL tables in HTML

I am currently using the code below to print out a MySQL table. However, when I am printing multiple rows the format is pretty ugly. What is a way or command I could create columns similar to the Tab key to make it more aesthetically pleasing?
$google= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
print "$cvalue\t <tr>";
}
print "<br>";
}
The query is printed using user input. using the
<form action="google.php" method="post">
command. I am also looking for a way to print this table on the same page as the HTML page that I accept the user input.
Your code is pretty good and only needs HTML added to it to make it look nicer. Using tables (this has not been tested but it should work):
$google = mysqli_query($query) or die(mysqli_error());
echo '<table>';
while($row = mysqli_fetch_assoc($google)) {
echo '<tr>';
foreach($row as $cvalue) {
print '<td>'.$cvalue.'</td>';
}
echo '</tr>';
}
echo '</table>';
This is a quick and dirty way to output a MySQL table, but if you also want the names of the columns to show up, things get quite a bit trickier. Also, if your results contain special characters such as ">", this can mess up the output. If that happens, just look up the documentation for htmlspecialchars().
If you want this table to show up on the same page as your form, just create a submit button and check the value in PHP like so:
if (isset($_POST['SubmitButtonNameHere']) { ... }
I'm not sure of the structure of your form so this code might need to be different to determine whether the form has been submitted. You can put the code for creating the table inside of the curly braces and place the code for your form either above or below the if statement.
<tr> is an HTML element used to add rows to a <table> element. The <td> element adds cells to those rows. Also, your code was using mysql_*. This was changed to mysqli_* as the mysql prefix is deprecated in PHP. Hope this answers your question.
Just use this code instead :)
$google= mysql_query($query) or die(mysql_error());
echo "<table><tbody>";
while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
echo "<tr><td>".$cname."</td><td>".$cvalue."</td></tr>";
}
echo "</tbody></table>";
Done :)

bad planing with mysql_fetch_assoc?

I am making a page where people can make posts. All of those posts are then shown in a table of 24 cells. I can have the last 24 posts shown with no problem, but now I don't know how to show the prior group(s) of posts. How can I fix my code to do that? I actually have this:
(I'm removing lines to make it easy to read)
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC";
// ---check everything is fine---- //
function retrieve_info($result)
{
if($row = mysql_fetch_assoc($result))
{echo $topic_if; echo $topic_subject; //and what I want in every cell
}
}
<table width="100%" height="751" >
<tr><td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td></tr>
<!-- repeat a few more times :-) -->
</table>
I though that by changing the variable $row with a number before the if statement would alter the output, but I still see the same data printed on screen. What should I do to be able to show next group of posts?
Thanks!!!
At some point when you have hundreds or thousands of records, you are going to want to paginate the results and not just select all records from the table.
To do this you will run one query per 24 records, your sql would be more like this:
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC
LIMIT 0, 24
";
and for the next 24,
LIMIT 24, 24
then
LIMIT 48, 24
and so on.
You would then make next/previous buttons to click which would refresh the page and dispay the next 24, or you would get the next results with an AJAX request and append the next 24 through the DOM.
This suggests having to take a slightly different approach then calling the same function from each table cell.
More like get the relevant 24 results based on the page number you are on, then loop through the results array and print out the table code with values inside it. Based on if the iterator of the loop is divisible by 4 (looks like your grid is 4x6), you print out new tags for the new row, and that sort of thing.
Search around a bit for pagination in php and mysql to get a sense of how this all fits together.
function retrieve_info($result)
{
while($row = mysql_fetch_assoc($result))
{
$topic_id = htmlspecialchars($row['topic_id']);
$topic_subject = htmlspecialchars($row['topic_subject']);
echo '<td>';
echo $topic_if;
echo $topic_subject; //and what I want in every cell
echo '</td>';
}
}

How to use zclip in MySql loop

Im trying to copy data to clipboard using zclip jquery. problem that im facing is when i copy it will copy everything in the database in field "name". but what i want is to copy a single name that user clicked. if the click another copy that. not the whole thing. can anyone tell me how to do this. thanks.
this is the zclip code
$(document).ready(function(){
$('p#copy').zclip({
path:'js/ZeroClipboard.swf',
copy:function(){return $('div#copy').text();}
});
MySQL loop:
$query = "SELECT * FROM names ORDER BY id desc";
$result= mysql_query($query) or die ('Mysql Error');
while($row = mysql_fetch_array($result)){
echo '<div id="copy">'.$row['name'].'</div>';
echo '<p id="copy">copy</p>';
}
zclip site: http://www.steamdev.com/zclip/
The HTML you generate is not valid which is hindering you in the end to achieve the result you're looking for: in HTML an ID can only be used once (see: Element identifiers: the id and class attributes). You are using the same ID for multiple DIVs. That can not work, as an ID must be unique across the whole HTML document.
Instead assign one ID to each DIV (e.g. by using a counter) and then apply the jquery function on the specific DIV only.
$idCounter = 0;
while($row = mysql_fetch_array($result))
{
$idCounter++;
echo '<div id="copy-div-', $idCounter, '">', $row['name'], '</div>';
echo '<p id="copy-p-', $idCounter, '" class="copy-click">copy</p>';
}
Code-Example: Create multiple IDs with a counter variable

Categories