How to categorized results from SQL, display 6 columns and set colorize on every category - php

I would like to ask for help on how to achieve this. I have this code below that pull records from DB and display it in 6 columns.
What I want to achieve is that, I want to display results on 6 columns but I want to categorize and set different color on every category.
let say i want to display the whole set of fruits starting from letter A with 6 columns with colors of gray, then below all letters starting with B with 6 columns with white color on background then below is C with gray colors in 6 columns. thanks.
<?php
fruits = $stmt->prepare("SELECT * FROM fruits ORDER by fruit_id ASC");
$fruits->execute();
$cols = 6;
do {
echo "<tr>";
for ($i = 1; $i <= $cols; $i++) {
$row = $fruits->fetch(PDO::FETCH_ASSOC);
if ($row) {
$fruit_id = $row['fruit_id'];
$fruit_name = $row['fruit_name'];
?>
<td>
<table>
<tr valign="top">
<td>
<?php echo '<input type="checkbox" id="fruit_id[]" name="fruit_id[]" value="' . $fruit_id . '"/>' . $fruit_name . "\n"; ?>
</td>
<td width="30"> </td>
</tr>
</table>
</td>
<?php
} else {
echo "<td> </td>";
}
}
} while ($row);
?>

Do it in the database:
SELECT fruits.*,if(#evenodd>0,'grey','white') AS color, #evenodd:=-1*evenodd AS dummy FROM (#evenodd:=1) as init, fruits ORDER by fruit_id ASC

Related

3x3 table with random images - PHP

I'm new here and need some help. I'm doing a web development course and have an assignment I need a hand with.
Basically, I want to send a query that picks 9 random records, and show the result as a 3x3 table.
Here's my code so far:
<div id="productgrid">
<?php
$query = mysqli_query($conn, "SELECT * FROM products
ORDER BY RAND()
LIMIT 9");
?>
<h2>Featured Products</h2>
<table>
<?php
while($products = mysqli_fetch_array($query)){
$file = $products['prod_img'];
$pid = $products['prod_id'];
$product_price = $products['prod_price'];
$image_id = $products['prod_id'];
$desc = $products['prod_desc'];
?>
<tr>
<div class="bigred">
<td class="bigred">
<?php echo '<b>'. "$product_price".'</b>'; ?>
</td>
</div>
</tr>
<tr>
<td>
<img src="<?php echo $file ?>" height = "150px" width="150px"/><br />
</td>
<div class="smallblack"
</tr>
<tr>
<td class = "smallblack">
<?php echo '<b>' . "$desc".'</b>'; ?>
</td>
</tr>
</div>
<?php } ?>
I can get it to generate the 9 random images but it puts them all directly under each other in one long column. Is there a way I can get them to display in 3 rows of 3?
I apologise if this is a dumb question, but I'm only starting out and appreciate the help.
Thank you :)
Pic attached as sample
<?php
// A counter which is incremented by one for each product row
// in the loop below.
$i = 0;
echo "<table>\n";
echo "<tr>\n";
while ($product = mysqli_fetch_array($query)) {
// Re-open HTML row, if $i is divisible by 3
if ($i++ % 3 == 0) {
echo "</tr><tr>\n";
}
// Escape the `src` attribute value as appropriate, according to the
// logic of your app. This is just a sample.
$img_src = htmlspecialchars($product['prod_img']);
echo <<<HTML
<td>
<div><img src="{$img_src}"/></div>
<div>{$product['prod_desc']}</div>
<!-- Put the rest of the fields here -->
</td>
HTML;
}
// Add cells for the rest of the columns (if any)
while ($i++ % 3 != 0) {
echo "<td></td>\n";
}
echo "</tr>\n";
echo "</table>\n";
?>
P.S.: I recommend using a template engine such as Smarty, or Twig. With the help of a template engine you can make the markup much more readable and maintainable.
You should change your table out put and use 3 <td> tags instead of just the one, like so...
<table>
<tr>
<?php
$counter = 0; // Needs to be outside of the loop...
while($products = mysqli_fetch_array($query)){
$file = $products['prod_img'];
$pid = $products['prod_id'];
$product_price = $products['prod_price'];
$image_id = $products['prod_id'];
$desc = $products['prod_desc'];
?>
<td class="bigred">
<?php echo '<b>'. "$product_price".'</b>'; ?>
<img src="<?php echo $file ?>" height = "150px" width="150px"/><br />
<?php echo '<b>' . "$desc".'</b>'; ?>
</td>
<?php
// Check to see if you have looped through three times and close the <tr> tag.
if($counter % 3 === 0) {
echo '</tr><tr>';
}
$counter++
?>
</table>
If there is more than 9 images, then it will keep creating table rows and cells and you will need more logic to handle that, however, this should work for 9 images.
A more elegant way to handle this would be to use <div> tags and floats, but since you are already using a table, I formatted it as such.

How to make usernames display in rows and columns in a Table

I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>
</table>";
} ?>
This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting ". $row['username'] ." in the other fields too, but that just duplicated it.
EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:
<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>
<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>
example:
try something like this
<table>
<tr>
<th>name</th>
<th>name</th>
</tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i == '0') echo "<tr>";
echo "<td>{$row['username']}</td>";
if ($i == '1') echo "</tr>";
$i++;
if($i =='2')$i='0';
}
?>
</table>
I think you're asking why the other fields in your "user" mysql table aren't showing up.
They aren't there because you only asked for the username:
$row['username']
If you have first/last name in your mysql table, you can retrieve it in the same way:
$row['firstname']
$row['lastname']
In your code you got the row as a key/value array like this:
$row = mysql_fetch_array($result)
The "key" is the name of the mysql column, like username, lastname, firstname. And the value is what is stored in the mysql table under that row/column, like joecool, smith, joe.

php +1 per set of content

I'm creating a series of content blocks. (4 in total).
It's stupid to create all for of them if you can let php create them for you.
So I created the first one and wrote a for loop that adds the content as long as it haven't a max number of 4.
Here it is:
<table>
<tr>
<?php
$counter =0;
for ($i=0; $i <= 3; $i++){
$counter++;
?>
<td>some content to repeat with id='$id++' and '$id++'</td>
<?php
if ($counter == 2){
echo '</tr><tr>';
$counter=0;
}
}
?>
</tr>
</table>
What this does is repeat the <td> two times and then end the rule and start a new one. Do this until you reach an amount of 4 (3 because 1=0)
The problem I have is that some of the content contains id's
The id's should also number up but only if the complete loop is repeated.
So $i++ should both have the same value if the loop runs once
So that would mean that if there are two id's in the first run they should both have the id1 and the next repeat should have id2
I have no idea how to do this.
The output should be:
<table>
<tr>
<td>
This content has id 1
And this content has id 1 to
<td>
<td>
This content has id 2
And this content has id 2 to
<td>
</tr>
<tr>
<td>
This content has id 3
And this content has id 3 to
<td>
<td>
This content has id 4
And this content has id 4 to
<td>
</tr>
</table>
M.
How about this
<table>
<?php
$id = 1
for ($i=0; $i < 2; $i++){
echo '<tr>';
for ($j=0; $j < 2; $j++){
echo 'This content has id $id';
echo' And this content has id $id too';
}
$id += 1;
echo '</tr>';
}
}
?>
</table>
Update: added an id counter.
Is this what you mean? You don't need $counter as you have $i keeping count for you.
<table>
<tr>
<?php for ($i=0; $i <= 3; $i++){ ?>
<td<?php
if ($i < 2) echo ' id="1"';
else echo ' id="2"';
?>>some content to repeat</td>
<?php if ($i == 1){ echo '</tr><tr>'; } ?>
<?php } ?>
</tr>
</table>

html auto rowspan in while

i have this result of sql:
SQL:
+-name-+---count---+
+ cat + 5 +
+ dog + 6 +
+------------------+
count of cat is 5 and count of dog is 6.
i want to create table with auto rowspan from this result
in this code rowspan must be get count of cat and get count of dog
HTML:
<table>
<th>NAME</th>
<th>COUNT</th>
WHILE ($sql=mysql_fetch_array($result))
{
<tr>
<td rowspan='$sql['cnt']'> $sql['name']; </td>
<td> $cnt++; </td>
<tr>
$cnt++;
}
</table>
Your code and images do not match, as they are reversed. Also, you are mixing php code with html.
try something like-
WHILE ($sql=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td rowspan="'.$sql['count'].'" valign="center">'.$sql['name'].'</td>';
for($i=1;$i<=$sql['count'];$i++){
echo '<td>'.$i.'</td>';}
echo '<tr>';
}

HTML table grid in PHP

I am currently doing some basic PHP and I am getting products from mysql and displaying each product with the corresponding details in a table.
Currently they are one underneath each other
Product1 Name
Product1 Price
Product1 Description
Product2 Name
Product2 Price
Product2 Description
Now I want to display them in a grid of 3 columns.
which means displaying 3 products side by side, then the 4th product is displayed underneath the 1st product and so on.
$displayAllProducts.=
"
<tr><td>Product Name : </td><td>$productName</td></tr>
<tr><td>Product Price : </td><td>$productPrice</td></tr>
<tr><td>Product Qty : </td><td>$productQty</td></tr>
<tr><td colspan =\"2\"><img src=\"$imagePath\" width = \"100\" height = \"100\"></td><td></td></tr>
<tr><td colspan =\"2\"><a href=\"singleProduct.php?pid=$productID&uid=$uid\">View Product<br/><br/><br/></td><td></td></tr>
";
<table>
<?php
echo $displayAllProducts;
?>
</table>
Without seeing your actual code that loops through your db rows, here is a general idea. This will make each $displayAllProducts its own table, nested in the main table <td>'s
$i=1; // start a general counter
while($i<$number_of_db_rows){
if($i%3 = 1) { // If number is 1,4,7,etc start a new row
$displayAllProducts.= "<tr>";
}
$displayAllProducts.=
"
<td> // put each db row inside a cell
<table> // create a bounding table
<tr><td>Product Name : </td><td>$productName</td></tr>
<tr><td>Product Price : </td><td>$productPrice</td></tr>
<tr><td>Product Qty : </td><td>$productQty</td></tr>
<tr><td colspan =\"2\"><img src=\"$imagePath\" width = \"100\" height = \"100\"></td><td></td></tr>
<tr><td colspan =\"2\"><a href=\"singleProduct.php?pid=$productID&uid=$uid\">View Product<br/><br/><br/></td><td></td></tr>
</table>
</td>
";
if($i%3 = 0) { // If number is 3,6,9,etc close the row
$displayAllProducts.= "</tr>";
}
$i++ // increase the counter to start again
} // ends the loop
<table>
<?php
echo $displayAllProducts;
?>
</table>

Categories