display series of products from mysql table - php

i am working on one project, where i need to display products from mysql table using php.
My design is like that, 5 products display in one row and so on.
So if there is 7 products in table, it should be 2 rows.
first row 5 products
second row 2 products
I am using <UL> and <LI> to display product and row like
<UL>
<li>first</li>
<li>2nd</li>
<li>3rd</li>
<li>4th</li>
<li>5th</li>
</ul>
<UL>
<li>6th</li>
<li>7th</li>
</ul>
I am wondering how to use foreach loop to get code like above.
If i use forloop with 5 increment i am unable to get products from table and display their name.
Thanks

echo '<ul>';
foreach ($yourResultSet as $index=>$data) {
echo '<li>' . YourOutput . '</li>';
if ($index % 5 == 0) {
echo "</ul><ul>";
}
}
echo '</ul>';

<ul>
<?php $i = 0; ?>
<?php while($row = mysql_fetch_array($result)): /* Used to loop through a resultset*/ ?>
echo "<li>".$row['data'].</li>";
<?php
$i++; // Increment by one every loop
if($i == 5){ // We've hit the fifth iteration
echo '</ul><ul>'; // Close the UL and open a new UL
$i = 0; // Set $i back to 0 and continue looping
}
endwhile;
?>
</ul>
Try that.

your best way of achieving this is to have your <li>'s have a specific width so that five of them fit in the parent container and that the following two or however many you have will drop to the next line.
<style>
#container {
width: 350px;
background: red;
padding:0;
margin: 0;
}
#list li
{
display: inline-block;
width: 50px;
height: 50px;
list-style-type: none;
background:blue;
margin: 2px;
padding: 0;
}
</style>
<div id="container">
<ul id="list">
<?php
while($row = mysql_fetch_array($result))
{
echo '<li>' . $row['data'] . '</li>';
}
?>
</ul>
</div>
You'll need to play with the sizes a bit but everything should work, and it you have 11 products this should do 2 rows of 5 and a third row of 1 product.

Related

i want to change the color of the dynamic div(by using backend am getting 4 divs ) using php

I want this type of appearance enter image description here
But am getting below result
enter image description here
I want different color for each div...by using while loop am getting those four divs..Here am writing css properties for one div those are reflecting for four divs
Declare an array for the four different colos (CSS classes for the colors), e.g.
$colors = array('green', 'blue', 'red', 'yellow');
Then declare the four classes in CSS, e.g.,
.green { background-color: green; /* and other styles etc */ }
.red { background-color: red; /* and other styles etc */ }
.blue { background-color: blue; /* and other styles etc */ }
.yellow { background-color: yellow; /* and other styles etc */ }
Then in you PHP script, use some loop counter like $i to pick a color from the array:
$i=0;
foreach($testimonials as $testimonial)
{
echo '<div class="' . $colors[$i++] . '">other content</div>';
}
I hope it helps!
Set four classes And before while loop set variable $i=1;
Now
<?php echo '
<style>
.div-1{background:#186aa9;}/*blue*/
.div-2{background:#e1404f;}/*red*/
.div-3{background:#f8bc34;}/*yellow*/
.div-4{background:#9cc03c;}/*green*/
</style>';
$i=1;
while($row = mysqli_fetch_assoc($result)){echo '
<div class="col-md-6 div-'.$i.'">
<div class=" kwikmint-message">
<p class="kwikmint-message-p">"'.$row['Message'].'"</p>
</div>
<div class="mint-testi-h5">
<h5>'.$row['Name'].'<br><span class="specialization">'.$row['Specilisation'].'</span></h5>
</div>
</div>';
$i++;
}?>

PDO echo data in table rows and cells

I'm trying to echo out data from my database using PDO and have it create a table with 4 columns and as many rows until there is no more data to echo out. Right now there should be 16 items in the database, so I should have 4 rows, but I can't get my data to echo in that format, 4 columns and 4 rows. It just echoes out in one rows and 16 columns. Thanks in advance for the help.
Here is the php code I have so far:
echo "<table>";
for($x = 0;$x<1;$x++){
foreach($result as $row){
echo "<td>{$row['question_name']}</td>";
}
echo "</tr>";
}
echo "</table>";
HTML tables are really bad for this. Instead, try using a plain list and use CSS to turn it into a grid
?>
<ul class="grid">
<?php foreach ($result as $row) : ?>
<li><?= htmlspecialchars($row['question_name']) ?></li>
<?php endforeach ?>
</ul>
and some CSS
.grid {
list-style: none;
width: 512px;
}
.grid li {
float: left;
width: 128px;
height: 128px;
}
If you really want to persist with the table approach, you need to detect every fourth entry and close / re-open a table-row, eg
?>
<table class="grid">
<tr>
<?php foreach ($result as $i => $row) : ?>
<td><?= htmlspecialchars($row['question_name']) ?></td>
<?php if (($i + 1) % 4 == 0) : ?></tr><tr><?php endif ?>
<?php endforeach ?>
</tr>
</table>

Displaying members using php three rows three column

I created three columns and three rows using li; I want to display one member in every li order by id.
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
My PHP code:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
foreach ($members as $member)
{
$html_ouput = '<li>';
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
This php code display nine results. Three columns three rows but how can I give every second li row a class middle? Any modification in my php code? I don't want to use javascript.
You need to concatenate the string to output
try this:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = '';
$i++;
if($i == 1)
$html_ouput .= '<li>';
else{
$i = 0;
$html_ouput .= '<li class="middle">';
}
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
What about this:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = ($i % 3 == 1 ? '<li class="middle">' : '<li>');
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
$i++;
}
echo '</ul>';
?>
Add a counter in the for each loop and test to see if the counter is divisible by 2... If it is then its an even number. If it is an even number add the class="middle".

display list items in multiple columns and limit the number of rows

i'm developing a php project in which i'm getting the list from the database and i need to display this data in html in 3 columns and 10 rows and add a button "more" in the end
<?php foreach ($items as $item) { ?>
<ul>
<li><a href="<?php echo $item['href']; ?>" >
<?php echo $item['name']; ?></a></li>
</ul>
<?php } ?>
I want it to be displayed as-
item1 item2 item3
item4 item5 item6
.
.
.
.
Maximum 10 rows
last row will be
item item "more" - only if there are more items in the list
"more" is just a link which will take me to some other page
Thanks for any help.
Add display:inline to li and use pseudo class to break it
ul{
margin:0
}
li{
display:inline
}
li:nth-child(3n):after {
content:"\A";
white-space:pre;
}
Script (To display more link)
$('#moreli').toggle($("li").size() > 6);
This enables the more link after 6 list items, you can change the count in the script.
Note: Reduce the list items to check the effect
DEMO UPDATED
<style>
.row {
width:100%;
float:left;
}
.item {
width:100px;
float:left;
}
</style>
<?php
//*** your array of items
$items = array("item1", "item2", "item3", "item4","item5", "item6", "item7", "item8", "item9", "item10");
$numItems = sizeof($items);
//*** max number of rows
$maxRows = 10;
$maxItems = 3 * $maxRows;
echo '<div class="row">';
for ($i=0; $i<$numItems;$i++) {
echo '<div class="item">'.$items[$i].'</div>';
if (($i+1) % 3 == 0) {
//*** if divisible by 3, close row
echo '</div><div class="row">';
}
if ($i == $numItems) {
//*** last item reached, close div
echo '</div>';
}
if ($i+1 >= $maxItems ) {
//*** max 10 row, add more button.
echo '</div><input type="submit" value="Add More">';
return;
}
}
?>
Try to use this:
<?php
$count=0;
foreach ($items as $item) { ?>
<ul class="col3">
<li>
<?php if($count > 9) { ?>
More</li></ul>
<?php break;
} else{ ?>
<a href="<?php echo $item['href']; ?>" >
<?php echo $item['name'];
$count=$count+1;
?></a>
<?php } ?>
</li>
</ul>
<?php } ?>
and following css for it
.col3
{
margin-left:-3%;
}
.cols3 li
{
width:30%;
margin-left:3%;
display:inline-block;
vertical-align:top;
}
.cols3 li a
{
display:block;
}

Organize div horizontally

My URL: illandeistudio
If you scroll halfway down you will see a list of categories (Accessories, dinning, lighting...)
They are currently vertical. How can I get these to display horizontally? Optimally they would be 4 per line. Below is the PHP code which places all the "categories" into a div "nelson"
Thanks! This has been hell!
.nelson {
float: left;
padding: 0 3px;
}
Then just style the padding from there. You can replace padding: 0 3px; with width: 80px; and adjust it from there if you want the columns to be even (might look better).
If you want it to be centered with 4 columns (you have 7 items so it would be 4 columns, 2 rows, and the last column would only have 1 row).... not sure why you would want it like that but its possible. You would create a around the code you wrote, create a width, and add the attribute:
style="margin: 0 auto; width: 916px;"
Then you would adjust the width of that to be the product of 7 * the width of .nelson
I'd recommend just adjusting padding though and leave the menu going horizontally. all the way across for all 7 items.
use like :
<?php if (count($this->document->shoppica_categories_arr) > 0) : ?>
<ul id="categories">
<?php $i = 0; ?>
<?php foreach ($this->document->shoppica_categories_arr as $category): ?>
<?php $i++ ?>
<li <?php if ($i % 4 == 0) : ?> class="ln"<?php endif ?>><?php echo $category->getName() ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
css file :
ul#categories li {
float: left;
}
ul#categories li.ln {
clear: left;
}
What Tallboy said. :) You may also need to set a width for the .nelson divs to get them floating correctly and lining up.

Categories