Turn "foreach" into "for" with structures - php

I have this block that prints some description and some product details.
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
I want to turn this into printing the description and only a number of product details so what I did is this
<?php
for ($i = 0; $i <=4; $i++) {
print_r($_additional[$i]);
}
?>
This prints only the description though. How can I also print the product details?
Thank you

You can access the details of the array with
<?php for ($i = 0; $i <=4; $i++) {
echo $_additional[$i]['label'];
echo $_helper->productAttribute($_product, $_additional[$i]['value'], $_additional[$i]['code']);
}

Related

Give Unique Class to each table row in loop

I have an PHP for loop; which looks at a number in database and outputs the tables rows to number of times.
Here is my HTML:
<?php
for($x=0; $x < 5; $x++){
$row_count =1;
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="yellow-sign" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
The above code display this:
How I can assign unique class to each of the rows?
Labelled Image
Example:
<?php foreach($data as $row) { ?>
<tr id="data<?php echo $row['id']; ?>"> </tr>
<?php } ?>
I hope it will help you.
If you are trying to get unique class to tr then do this
for ($x = 0; $x < 5; $x++) {
$row_count = 1;
$randClass = rand(1111,9999).$row_count;
?>
<table class="table table-bordered" id="ticktable">
<tr class="<?php echo $randClass; ?>">
<?php
for ($w = 0; $w < 5; $w++) {
?>
<td>
<img class="yellow-sign <?php echo $randClass; ?>" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count++;
}
rand(1111,9999) will generate random number between 1111 and 9999
EDIT
i have also added $row_count so that the number can not be repeated even if there is large data. What I've done is: <tr class="<?php echo rand(1111,9999).$row_count; ?>">
UPDATE
first store the random string in a variable $randClass = rand(1111,9999).$row_count;
then echo out where you want the variable like this:
$randClass = rand(1111,9999).$row_count;
and <img class="yellow-sign <?php echo $randClass; ?>" >
Here is the updated code for you. Check this
<?php
$color_class[] = array("yellow-sign","red-sign","green-sign","blue-sign","black-sign");
$row_count = 0;
for($x=0; $x < 5; $x++){
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="<?php echo $color_class[$row_count]; ?>"
src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>

Foreach display only first result

How can I make this foreach line only go through the first result?
<?php foreach ($collection as $product) : ?>
I tried the following, but that does not work. It than does not display any results:
<?php foreach (array_slice($collection, 0, 1) as $product) : ?>
EDIT:
The following works perfect:
<?php
$i = 0;
foreach ($collection as $product) :
if($i < 1) {
Inner content
}
$i++;
endforeach;
?>
Total current code:
<tbody>
<tr>
<td class="image" rowspan="3">
<?php $i = 0; foreach ($collection as $product) : if($i < 1) {?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75) ?>" alt="<?php echo $this->htmlEscape($product->getName()) ?>" width="75" height="75" />
<?php } $i++; endforeach ?>
</td>
<td class="order" colspan="2">order</td>
<td class="exclTax">excl. vat</td>
<td class="inclTax">incl. vat</td>
</tr>
<?php foreach ($collection as $product) : ?>
<tr>
<td class="select"><input type="radio" name="featured_1807" id="featured_1807_1349567899" value="3071895, IM" data-product-id="3071895" data-product-sup="IM"></td>
<td class="title"><?php echo $this->htmlEscape($product->getName()) ?></td>
<td class="price"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
<td class="priceIncl"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
</tr>
<?php endforeach ?>
</tbody>
How can I achieve that?
Why bother with a loop if you are only interested in the first occurance of $collection
You could do this instead and probably not have to change any code you currently have inside your loop
<?php
$product = $collection[0];
Code you had in the loop
?>
If its not a numeric key then you can use
<?php
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
... html stuff
// and then do again for your second loop
// if you only want the first occ of that as well
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
reset($collection); // Resets the array's internal pointer to the first element
$product = current($collection); // Returns the current element which is now first element
As far as I'm concerned, this is the best solution I would ever go for if I need to only extract the first element from an array.

For loop inside Foreach

I got a foreach which loops through my database for each user.
<?php
foreach($users as $user) {
?>
<tr>
<td>
<?php
for ($i=1; $i < 52; $i++) {
echo $i;
}
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
This loop through database and echos the username, now I tried to build a for loop inside this so that every user has a number, I took as an example a very basic for loop ( it will be changed later).
The problem is that I get all numbers printed out for each user, but I just want to print out the number once for a user. How do I solve this.
If you want unique index number for user, you do not need extra loop.
Just add an increment variable:
And increment it in existing loop.
<?php
$i=0;
foreach($users as $user) {
++$i; // For printing first user, it will be 1 not 0.
// Therefore, increment is placed before printing name.
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php
}
?>
This should be enough to achieve what you're trying to do :
Your for() isn't needed since foreach() already create a loop, you just have to use this loop to increment a value (here called $i) then display it.
Also you should avoid to open your php tags ten thousands times for a better visibility into your code :)
<?php
$i = 0;
foreach($users as $user) {
$i++;
echo'<tr>
<td>'.$i.'</td>
<td>'.$user['firstname'].'</td>
</tr>';
?>
<?php
$i=1;
foreach($users as $user) {
?>
<tr>
<td>
<?php
echo $i++;
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
Try to use the key in foreach
<?php foreach ($users as $key => $user) : ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php endforeach; ?>

php foreach break table

Im using tables to store content that's dynamically loaded. It's for a reservation form which will be responsive. What I'm looking to do is break each table row into two if there are more than 5 columns in order for the mobile version to fit on screen.
I'm sure this can be achieved by extending what I already have but can't get it to work.
Here's my current code:
<table>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach ?>
</tr>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach ?>
</tr>
</table>
I'd need to break the loop for each row tr after 5 loops, then add a new row underneath.
I've been experimenting with
$max_loop = 5;
$count = 0;
But no luck so far.
I prefer to reorganize data:
<?php
$availDates = array();
foreach ($hostel->getAvailableDates() as $date) {
$availDates[] = $date;
}
$maxCols = 5;
$chunked = array_chunk( $availDates, $maxCols );
?>
<table>
<?php
foreach ($chunked as $chunk) {
?><tr>
<?php foreach ($chunk as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach ($chunk as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach; ?>
</tr><?php
}
?>
</table>
Look at the mod operator. It should give you what you need.
if($count % $max_loop == 0)
I hope this may help you. thanks.
<?php
$avDates = $hostel->getAvailableDates();
echo "<table><tr>";
foreach($avDates as $i=>$date){ {
if ($i == $max_loop) {
echo "</tr><tr>";
}
echo "<td>".($date->getAvailable() ? '<b class="avail tick">Available</b>' : '<b class="avail cross">Unavailable</b>')."</td>";
}
echo "</tr></table>";
?>
If the value returned by getAvailableDates is an array, you could use a for loop instead of a foreach, and check if the current index is a multiple of five, so you don't have to keep track of the count variable
$avDates = $hostel->getAvailableDates();
for ($i = 0; $i < count($avDates); $i++) {
$date = $avDates[$i];
//do your staff
//if multiple of five add another tr
if ($i % 5 == 0) {
}
}

php loop from one table then loop from another table

I tried searching but didn't find a good answer to my question. I have the following code for a loop:
<?php
$i=0;
while ($i < $num) {
$f0=mysql_result($sql,$i,"id");
$f1=mysql_result($sql,$i,"title");
$f2=mysql_result($sql,$i,"post");
?>
<p><?php echo $f1; ?></p>
<p><?php echo $f2; ?></p>
<p>Categories:</p>
<?php
$i++;
}
?>
<?php
$i=0;
while ($i < $num1) {
$f3=mysql_result($sql1,$i,"category");
?>
<?php echo $f3; ?>
<button>Edit</button>
<?php
$i++;
}
?>
The problem is that it is only looping up to <p>Categories</p>. It does not loop the categories and edit part which is the $f3 variable and Edit button. The categories table is a separate table so I had set up a different query ($sql1) than the posts table. How can I include those parts into the loop so they show for each post? I tried moving the $i++ part to the bottom but that just showed me the same post infinitely. Thank you for your help.
Embeded loops
<?php
$i=0;
while ($i < $num) {
$f0=mysql_result($sql,$i,"id");
$f1=mysql_result($sql,$i,"title");
$f2=mysql_result($sql,$i,"post");
?>
<p><?php echo $f1; ?></p>
<p><?php echo $f2; ?></p>
<p>Categories:</p>
<?php
$j=0;
while ($j < $num1) {
$f3=mysql_result($sql1,$j,"category");
echo $f3;
$j++;
}
?>
<button>Edit</button>
<?php
$i++;
}
?>
Udan's answer was correct but wasn't looping through my categories for each post for some reason. I decided to use a different loop and got it working. Posting here in case anyone else has the same problem.
echo "<h2>Posts</h2>";
$sql = mysql_query("SELECT id, title, post FROM posts") or die(mysql_error());
while ($test = mysql_fetch_array($sql)) {?>
<table><tr><td><input type="hidden" name="id" value="<?php echo $test['id']; ?>" /></td></tr>
<tr><td><?php echo $test['title']; ?></td></tr>
<tr><td><?php echo nl2br($test['post']); ?></td></tr></table>
<?php
echo "Categories:";
$sql1 = mysql_query("SELECT categories.* FROM categories INNER JOIN post_category ON categories.id=post_category.cat WHERE post_category.post='".$test['id']."'") or die(mysql_error());
while ($test1 = mysql_fetch_array($sql1)) {?>
<table><tr><td><?php echo $test1['category']; ?></td></tr></table>
<?php
}
?>
<button>Edit</button>
<?php
}
?>

Categories