how to increment CSS ID using PHP programmatically - php

how can i increment my css ID using php programmatically?
foreach ($query_cat->result() as $row_cat)
{
$row_cat_id = $row_cat->id;
echo '<div class="product-wrapper">';
echo '<div id="product-header" class="product-header">';
echo $row_cat->title;
echo '</div>';
echo '</div>';
$query_prod = $this->db->get_where('products', array('category_id'=>$row_cat_id));
foreach ($query_prod->result() as $row_prod)
{
$row_prod_id = $row_prod->id;
echo '<div id="product-contents" class="product-contents">';
echo $row_prod->title.' '.$row_prod->id;
echo '</div>';
}
}
what i want to happen is to increment the id product-header and product-contents depending on the numbers of rows generated
something like this
product-header1, product-header2, product-header3....
product-contents1, product-contents2, product-contents3....
thanks!

Just initialise an incremental variable before the foreach.
$i = $j = 0;
foreach ($query_cat->result() as $row_cat) {
$i++;
$row_cat_id = $row_cat->id;
echo '<div class="product-wrapper">';
echo '<div id="product-header'.$i.'" class="product-header">';
...
foreach ($query_prod->result() as $row_prod) {
$j++;
$row_prod_id = $row_prod->id;
echo '<div id="product-contents'.$j.'" class="product-contents">';
...
}
}

$i = $j = 1;
foreach (...) {
printf('<div id="product-header-%u" class="product-header">', $i++);
foreach (...) {
printf('<div id="product-contents-%u" class="product-contents">', $j++);
}
}

Related

PHP Loop on every value on multidimensional array X times [duplicate]

I have a foreach statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?
You can use a counter and the modulo/modulus operator as per below:
<?php
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
foreach ($featured_projects as $i => $fp) {
echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.
Otherwise you would have to add a counter.
http://php.net/manual/en/language.operators.arithmetic.php
add a counter in this loop and check if counter equals three and apply class.
Using a counter and modulo operator this is easy to implement
<?php
foreach($featured_projects as $fp) {
if(++$i % 3 === 0) {
$class = ' something';
} else {
$class = '';
}
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
$i = 0;
foreach($featured_projects as $fp) {
echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
echo $fp['project_name'];
echo '</div>';
}
?>
What leaves your code mostly in tact would be
<?php
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>
But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.
<?php
$counter = 0;
foreach ($featured_projects as $fp) {
echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
You can add a counter in loop ...try the following...
<?php
$i = 0;
foreach($featured_projects as $fp) {
$i = ++$i;
if(($i%3) == 0)
{
$class1 = 'test1';
}
else
{
$class1 = 'test2';
}
echo '<div class="'.$class1.'">';
echo $fp['project_name'];
echo '</div>';
}
?>
This is the working version, sorry for my prev version:
<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";
$class[0] = "class1";
$class[1] = "class2";
$i=0;
foreach($featured_projects as $fp) {
$i++;
if ($i == 3) {
$c = $class[1];
$i=0;
} else {
$c = $class[0];
}
echo "<div class=\"$c\">";
echo $fp['project_name'];
echo "</div>\n";
}
?>
Produces:
<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>

how to filter data from html table

This is a code i used to prase a data through html dom php and echo a data in table form
all data came in perfect table form. but i want to show only 5 out of all data echo in table.at last i used table to echo a content or data i want only to echo 5 data out of all showing out but i am not getting any idea to code this.. any one can help me to do this?
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.discoverhongkong.com/us/see-do/events-festivals/events-calendar/index.jsp');
$eventRowData = array();
class eventRecord
{
public $dates;
public $eventDescription;
}
;
foreach ($html->find('#event_table tr') as $eventItem) {
$eventDateArray = array();
$oneEventData = new eventRecord;
foreach ($eventItem->find('.date') as $eventDate) {
$oneDateData = array();
$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($eventDate);
$dom->preserveWhiteSpace = true;
$hTwo = $dom->getElementsByTagName('div')->item(0);
foreach ($hTwo->childNodes as $dateElement) {
array_push($oneDateData, $dateElement->nodeValue);
}
//print_r ($oneDateData);
array_push($eventDateArray, $oneDateData);
}
//
$oneEventData->dates = $eventDateArray;
$eventData = null;
foreach ($eventItem->find('.event_name') as $eventName) {
$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($eventName);
$dom->preserveWhiteSpace = true;
$baseLink = "http://www.discoverhongkong.com";
$img = $dom->getElementsByTagName('a')->item(0);
$relativeLink = $img->attributes->getNamedItem("href")->value;
$eventLink = $baseLink . $relativeLink;
$eventData = '<strong>' . $img->textContent . '</strong><br />';
$oneEventData->eventDescription = $eventData;
//echo $oneEventData->eventDescription;
}
array_push($eventRowData, $oneEventData);
}
$arr = array_values($eventRowData);
//Print_r ($eventRowData);
//echo "----------";
//echo $arr[0]->eventDescription;
//echo "----------";
echo '</br>';
echo '<h2>Events In HongKong</h2>';
echo '<table class="table-bordered">';
echo '<tr>';
echo '<th class="abc">EVENT NAME</th>';
echo '<th>START DATE</th>';
echo '<th>END DATE</th>';
echo '</tr>';
foreach ($arr as $xyz) {
//print_r ($xyz);
echo '<tr>';
echo '<td>';
echo ($xyz->eventDescription);
echo '</td>';
//$mydates = array_values($xyz->dates);
//print_r ($mydates);
foreach ($xyz->dates as $datevalue) {
//echo '<tr>';
echo '<td>';
foreach ($datevalue as $datevalueitem) {
echo $datevalueitem;
echo '/';
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
Which of these loops is the one you want to restrict? Whichever one it is, the structure would be the same. Something like this:
$i = 0;
foreach ($collection as $item) {
$i++;
if ($i > 5) {
break;
}
// the rest of your loop code
}
So you're basically storing in $i (or whatever you want to call the variable) a count of how many times the loop has executed. After 5 times, you use break; to exit the loop, regardless of how many records remain.

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

start end div after some elements php

I cannot solve problem with starting ending divs after couple of elements from array.
What i want to get is something like that:
<div>
element1
element2
element3
element4
</div>
<div>
element5
element6
element7
element8
</div>
<div>
element9
element10
</div>
Here is my php code:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 1;
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 OR $count == 1){
echo '<div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
Try something like this
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 0;
echo '<div>';
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 && $count!=0){
echo '</div><div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
echo '</div>';
Okay I am not familiar with arrays and maybe something like this would work:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$i=0;
echo '<div>'
if (i<3) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>3 && $i<7) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>7 && $i<10) {
echo '$array[$i]';
$i++;
}
echo '</div>';

While loop : get first div to add active class

i want to add active class in while loop first div.
but its not working
$ban_sql = mysql_query("SELECT * FROM banners");
while ($row = mysql_fetch_assoc($ban_sql)){
echo '<div class="item ';
if($row <= 1) {
echo 'active ">';
}
else
{
echo '">';
}
echo '<img src="'.$row['banner_img'].'"/> </div>';
}
This should do the trick:
$ban_sql = mysql_query("SELECT * FROM banners");
$count = 0;
while ($row = mysql_fetch_assoc($ban_sql)){
echo '<div class="item ';
if($count == 0) {
echo 'active ">';
}
else
{
echo '">';
}
echo '<img src="'.$row['banner_img'].'"/> </div>';
$count++;
}
$row will contain a MySQL rowset so you need a separate variable to store the current row iteration count.

Categories