while loop first output a different class - php

I use a slider from codyhouse.co and integrate it in wordpress. My goal is to count the slides and output a navigation where the first navigation bullet gets the class selected.
The part of counting slides and outputting the navigation works. But i don't know how to to setup the code to get the first bullet in the navigation the class selected. This is my post loop:
while ($my_query->have_posts()) : $my_query->the_post();
<li if($counter==0) { echo 'class="selected"'; $counter++; }>
<div class="inner-hero">
$titleslider = get_post_meta($post->ID, 'textslider', true); ?>
<h2>echo $titleslider; ?></h2>
</div>
the_post_thumbnail();
</li>
$slider = $my_query->post_count;
if ($slider > 1) {
while ($i < $slider) {
$output .= "<li class=''>" ."<a href='#0'>" . "</a></li>";
$i++;
}
}
endwhile;
While loop in the post loop:
while ($i < $slider) {
$output .= "<li class=''>" ."<a href='#0'>" . "</a></li>";
$i++;
I use the class selected also for the slides, that works fine.
So what i did is, count the slides, check if there are more than one slide, if this is true show the navigation. But my next goal is to get the first li in the navigation the class selected in the while loop.
naviagtion outside the post loop.
<ul class="cd-slider-navigation">
echo $output;
</ul>

Suppose you have defined class 'test1' for the selected one and class 'test' for the other links.
Before your while loop starts, you'll have to define a variable named $j equal to 1.
Then the code goes here,
$j=1;
while ($i < $slider) {
if($j==1)
$output .= "<li class='test".$j."'>" ."<a href='#0'>" . "</a></li>";
else
$output .= "<li class='test'>" ."<a href='#0'>" . "</a></li>";
$i++;
$j++;
}

Related

foreach loop where is the place of the closing ul?

I would like to display all existing categories and all posts.
I wrote a foreach loop, but I don't know where is the place of the closing ul tag.
My code:
<?php
$current_cat = 0;
$curr_nb_item = 0;
foreach($blog_widget as $row)
{
if($current_cat != $row->category_id)
{
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
else {
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
}
?>
Now I got this result in html:
<ul>1
<li>
cat 1 post n
</li>
<li>
cat 1 post h
</li>
<ul>2
<li>
cat 2 post x
</li>
<li>
cat 2 post y
</li>
I tried a lot of variation without result. I hope somebody could help for me. Many thanks.
This may not be the cleanest, but it should work. Modify the top of your if statement to look like this:
if($current_cat != $row->category_id)
{
if ($row != $blog_widget[0])
{
echo "</ul>";
}
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
// etc...
And then after the closing brace for your foreach loop:
echo "</ul>";

PDO MySQL product loop

Im a real beginner when it comes to queries and PDO, i need some assistance in a project i am busy with. What i want to accomplish is to display products from the database. However the template style i am using forces me to show 3 products per row, is there a way to show 3 products per row and loop the code once there is more than 3 (if there is 5 products, the first 3 will display in the first row, and the rest in the second).
Here is the template i am using, note the div class "top-box", this forces that only 3 products can be shown per row.
<div class="top-box">
<?php
$sql = "SELECT * FROM _products WHERE category = '$cat'";
$result = dbConnect()->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$cols = $result->columnCount(); // Number of returned columns
// Generate ADS Feed for each ROW
foreach($result as $row) {
echo '<div class="col_1_of_3 span_1_of_3">
<a href="product.php?i=' . $row['model'] . '">
<div class="inner_content clearfix">
<div class="product_image">
<img src="images/' . $row['model'] . '.jpg" alt=""/>
</div>
<div class="price">
<div class="cart-left">
<p class="title">' . $row['name'] . '</p>
</div>
<div class="clear"></div>
</div>
</div>
</a>
</div>';
}
} else {
echo "<p>No products available at this moment, contact us for more information!</p>";
}
?>
<div class="clear"></div>
</div>
You can solve it like this:
<?php
if ($counter % 3 == 0 && $counter!=$total_row_fetched) {
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="top-box">';
}
if($counter==$total_row_fetched){
echo '<div class="clear"></div>';
echo '</div>'; // it will close the last open div
}
++$counter;
?>
You can use a count variable inside your products foreach-loop. Every three products you can close the top-box and open a new one (This is an assumption as I don't know exactly how your styles work).
At the end of the foreach-loop:
if ($count != 0 && $count % 3 == 0) {
echo '<div class="clear"></div>';
echo '</div>'; // close last .top-box
echo '<div class="top-box">';
}
++$count;
I don't see how your question is connected to PDO. Keep in mind that using unescaped variables inside a query is potentially dangerous. Have a look here for some help: https://stackoverflow.com/a/60496/2516377
Add a counter and use % arithmetic operator to calculate column number.
$counter=0;
foreach (...) {
$column_number=$counter % 3;
$counter++;
}

Php foreach group items into a container

I can't figure out how to group my subcategories into a container after every fifth record.
for example I would like to output my subcats like this
<ul>
<li>
Main category
<div>
<div class="subcontainer">
sub1
sub2
sub3
sub4
sub5
</div>
<div class="subcontainer">
sub6
sub7
sub8
sub9
sub10
</div>
</div>
</li>
</ul>
I tried this:
<?php
$i=0;
foreach ($cat->sub as $child) {
if($i % 5 == 0){
echo '<div class="subcontainer"><div>'.$child->name.'</div></div>';
} $i++;
}
?>
But this will output only the 5th elements in the array.
You need to always print your $child->name variable and on every 5th item also print the </div><div> part. You almost have it.
$i=0;
echo '<div class="subcontainer">';
foreach($cat->sub as $child) {
$i++;
echo $child->name; // I'm assuming this is what contains your "sub1"-"sub10"
if ($i % 5 == 0) {
echo '</div><div class="subcontainer">';
}
}
echo '</div>';
You can also get $iwith foreach ($cat->sub as $i=>$child)

Order data in three columns instead of one

At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.

Outputting array contents as nested list in PHP

I have the array array ( [0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10)) and I would like to display it like this:
<ul>
<li>
<a href=""/>FIRST ELEMENT OF THE array ==> 1</a>
<a href=""/>2ND ELEMENT OF THE TAB ==> 2</a>
<a href=""/>3THIRD ELEMENT==> 3</a>
<a href=""/>FORTH ELEMENT OF THE TAB ==> 4</a>
<a href=""/>FIFTH ELEMENT==> 5</a>
</li>
<li>
<a href=""/>6th ELEMENT==> 6</a>
<a href=""/>7th ELEMENT OF THE TAB ==> 7</a>
<a href=""/>8th ELEMENT==> 8</a>
<a href=""/>9th ELEMENT OF THE TAB ==> 9</a>
<a href=""/>10th ELEMENT OF THE TAB ==> 9</a>
</li>
</ul>
How can I achieve this in PHP? I am thinking of creating a sub array with array_slice.
Updated to take into account your actual array structure
Your solution is a simple nested foreach.
$tab = array(array(1,2,3,4,5), array(6,7,8,9,10));
echo '<ul>';
foreach ($tab as $chunks) {
echo '<li>';
foreach($chunks as $chunk) {
echo '' . $chunk . '';
}
echo '</li>';
}
echo '</ul>';
try
echo "<ul>";
$i=0;
$theCount = count($tab);
while($i<$theCount){
echo "<li>";
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
$i++;
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
echo "</li>";
$i++;
}
echo "</ul>";
Here is another way to do this (demo here):
<?php
$tab = array(1,2,3,4,5,6,7,8,9,10);
//how many <a> elements per <li>
$aElements = 2;
$totalElems = count($tab);
//open the list
echo "<ul><li>";
for($i=0;$i<$totalElems;$i++){
if($i != 0 && ($i%$aElements) == 0){ //check if I'm in the nTh element.
echo "</li><li>"; //if so, close curr <li> and open another <li> element
}
//print <a> elem inside the <li>
echo "<a href =''>".$tab[$i]."</a>";
}
//close the list
echo "</li></ul>";
?>
Tip explanation: $i%n (mod) equals 0 when $i is the nTh element (remainder of division is 0)
EDITED: made a general solution
<?php
for($i = 0 ; $i < count($tab) ; $i += 2) {
echo "<a href>" . $tab[$i] . "</a>";
echo "<a href>" . $tab[$i+1] . "</a>";
}
?>
Like that.
try this:
$sections = array_chunk(array(1,2,3,4,5,6,7,8,9,10), 2);
echo '<ul>';
foreach($sections as $value)
{
echo '<li>';
echo '<a href=""/>'.$value[0].' ELEMENT OF THE TAB ==> '.$value[0].'</a>';
echo '<a href=""/>'.$value[1].' ELEMENT OF THE TAB ==> '.$value[1].'</a>';
echo '</li>';
}
echo '</ul>';

Categories