xml to php table alternating row color - php

Trying to display an html table from and xml from php, getting error when trying to alternate the row base on even and odd mostly for styling the table.
foreach($bookdata as $book) // loop through our books
{
$i = 0;
if($i%2 == 0)
{
$class = 'even';
}
else
{
$class = 'odd';
}
{
echo <<<EOF
<tbody>
<tr class='$class'>
<td>{$book->date} </td>
<td><a href='http://www.website.com{$book->dataNo}.html'>{$book->Name}</td>
<td><a href='http://www.website.com/-{$book->authorcodeNo}.html'>{$book->author}</td>
</tr>
}
$i++;
}
EOF;
}
echo '</tbody>';
echo '</table>';
Any help most welcome

$i = 0;
foreach($bookdata as $book) // loop through our books
{
...
...
//and at end of foreach
$i++;

You are redeclaring $i inside of your for loop so it's never going to actually increase, just get reset to 0 every single time. Also, not sure what's up with some of your curly braces as there's not enough code to see it all as far as I can tell... start by moving your variable declaration outside the for loop though!

You are reseting the $i to 0 on every loop.
Remove
$i = 0;
from your code. And I didn't notice this before but the EOF is misplaced. Here is a full working solution
foreach($bookdata as $book) // loop through our books
{
if($i%2 == 0) { $class = 'even'; }
else { $class = 'odd'; }
echo <<<EOF
<tbody>
<tr class='$class'>
<td>{$book->date} </td>
<td><a href='http://www.website.com{$book->dataNo}.html'>{$book->Name}</td>
<td><a href='http://www.website.com/-{$book->authorcodeNo}.html'>{$book->author}</td>
</tr>
EOF;
$i++;
}

try put the $i=0 out of foreach loop.

Related

PHP - How to use a div to wrap all items Inside a foreach loop

I have been scratching my head for half hour now trying to figure this out
I have a wrapper div inside a foreach loop and I'm trying to force it to echo one and not duplicate
foreach ($apples as $apple) {
//echo only once bellow
<div class="Wrapper">
//echo only once Above
echo $apple;
//echo only once bellow
</div>
//echo only once Above
}
I do not wish to move my Wrapper div outside the foreach, It is very important for the div to be inside the foreach and to be without duplicates.
You didn't mention but If you need to put Wrapper div only if you have any count in $apples variable then simply check with if(count($apples)) and then put Wrapper
<?php
if(count($apples)){
echo '<div class="Wrapper">';
foreach ($apples as $apple) {
echo $apple;
}
echo '</div>';
}
?>
Note: I know this is not good way to write but as per his condition I am suggesting this code.
I don't know why you don't want to put div outside foreach loop but anyway you can use the following code to achieve that...
<?php
foreach ($apples as $apple) {
static $i = 0;
if ($i == 0) {
echo "<div class='Wrapper'>";
}
echo $apple;
$i++;
if ($i == count($apples)){
echo "</div>";
}
}
?>
Maybe this will help since you want to keep it inside foreach loop
$numItems = count($arr);
$i = 0;
foreach($arr as $ar) {
$i = $i+1;
if ($i == 1) {
echo '<div class="class">';
}
// do your stuff
if($i == $numItems) {
echo "</div>";
}
}

PHP Foreach loop not working with variable

I'm sorry, if this is duplicate post, but somehow i can't get my loop working with variable. if i echo this inside foreach i get my data but if i use variable i get data for only the first item from my array.
my code `
$user - My arrray
$count = $user['count'];
$echo = "";
foreach($user as $val => $key)
{
if($val == 'true')
{
for($x = 0; $x < $count; $x++)
{
$name = $user[$val][$x]['name'];
$id = $user[$val][$x]['id'];
$staatus = $user[$val][$x]['status'];
$feed = $user[$val][$x]['feed'];
/* Now when am using only echo, instead of $echo it will work flawlessly,
but i want to echo it using an variable called $echo.
I call the variable on my html page where the forms are,
but i'm only getting data for the first element.
Short, i want to display my data under the form.*/
$echo = "
<table class='table'>
<tr>
<th>NIMI</th>
<th>ID</th>
<th>STAATUS</th>
<th>FEED</th>
</tr>
<tr>
<td>$name</td>
<td>$id</td>
<td>$staatus</td>
<td>$feed</td>
</tr>
</table>
";
}
}
}
<?php echo $echo; ?> for calling.
I'm quite a beginner in this thing so maybe someone could help. I also searched around, tried different things but it's not working. I also tried to display data without for loop, but it's not working either.
Also if there's any improvments i could do with my code please tell me. Like make it shorter etc, i'm here to improve so why not :D
You're missing the . in your $echo .= "
$echo .= "
<table class='table'>
<tr>
<th>NIMI</th>
<th>ID</th>
<th>STAATUS</th>
<th>FEED</th>
</tr>
<tr>
<td>$name</td>
<td>$id</td>
<td>$staatus</td>
<td>$feed</td>
</tr>
</table>
";

Variable Defined in Else Is Ignored in If

I'm trying to build divs based on unique values. So for each line, the script checks the team name against $tempTeam. If they're equal, the line gets added to the table in the existing div. If they're not equal, a new div and table is created, and the line is added there.
However, the if part isn't recognizing when $tempTeam is equal to the team so it never runs, even though the else part is properly setting $tempTeam. So I'm wondering why the variable is working for the else part of the code, but not the if part.
Here's the full code, although the trouble begins when I first define $tempTeam. Thanks for any help.
<?php
$csv = file_get_contents('schedules.csv');
$csv_array = explode("\n", $csv);
unset($csv_array[count($csv_array) - 1]);
$headers = explode(",", $csv_array[0]);
// iterate through all lines of CSV, skipping first header line
for($i=1;$i<count($csv_array);$i++) {
$line = explode(",", $csv_array[$i]);
// iterate through all headers and assign corresponding line value
foreach ($headers as $index => $header){
$parsed_array[$i][$header] = $line[$index];
}
}
// create divs and tables
$tempTeam = '';
foreach ($parsed_array as $i => $match) {
if ($tempTeam == $match['Team']) {
echo "
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
else if ($tempTeam == '') {
$tempTeam = $match['Team'];
echo "
<div class=\"schedule\" data-container=\"$match[League]\">
<table>
<thead>
<tr>
<th colspan=\"4\">$match[Team]</th>
</tr>
<tr>
<th>Date</th><th>Result</th><th>Location</th><th>Opponent</th>
</tr>
</thead>
<tbody>
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
else {
$tempTeam = $match['Team'];
echo "
</tbody>
</table>
</div>
<div class=\"schedule\" data-container=\"$match[League]\">
<table>
<thead>
<tr>
<th colspan=\"4\">$match[Team]</th>
</tr>
<tr>
<th>Date</th><th>Result</th><th>Location</th><th>Opponent</th>
</tr>
</thead>
<tbody>
<tr><td>$match[Date]</td><td>$match[Result]</td><td>$match[Location]</td><td>$match[Opponent]</td></tr>
";
}
}
echo "
</tbody>
</table>
</div>
";
?>
For a start, look at the first line of the else clause:
$tempTeam == $match['Team'];
I'm pretty certain that's not what you wanted to do, it seems to me that assignment would probably be a better choice than comparison.
What you're doing is no different to:
$a = 1;
$a == $a + 1;
print ($a);
which will still output 1 rather than 2. If you want do do assignment, it should have just the one = character:
$tempTeam = $match['Team'];

How to limit foreach loop to 3 results as icons in PHP?

I have this piece of code that is showing all results as icons, but it shows nearly 50 icons in each loop. I searched many ways to change it to 3 icons in each loop unsuccessfully. How can I do that?
foreach ($categoryItems as $item) {
if ($item['categoryId'] != $category['id'])
continue;
$itemXML = $items->findItemInXML($item['itemId']);
$subContent .= "<a class=\"thumbnail\" ><img src=\"images/gameIcons/".$items->getIcon($itemXML).".png\" alt=\"Item icon\" align=\"top\" />";
}
$subContent .= "</td>
<td class=\"tablePreview ui-state-hover\">
More information
</td>
</tr>";
Since you have the continue condition, my suggestion to use a for loop is less relevant. Instead you can use a counter variable to count how many icons have been printed. Once that counter reach 3 it breaks the loop.
(Didn't tested it)
$printedIcons = 0;
foreach ($categoryItems as $item) {
if($printedIcons == 3)
break;
if ($item['categoryId'] != $category['id'])
continue;
$itemXML = $items->findItemInXML($item['itemId']);
$subContent .= "<a class=\"thumbnail\" ><img src=\"images/gameIcons/".$items->getIcon($itemXML).".png\" alt=\"Item icon\" align=\"top\" />";
$printedIcons++;
}
You can use a count variable and while loop to iterate until 3rd icon is executed.
foreach ($categoryItems as $item) {
$count=0;
while(count<=2) {
if ($item['categoryId'] != $category['id'])
continue;
$itemXML = $items->findItemInXML($item['itemId']);
$subContent .= "<a class=\"thumbnail\" ><img src=\"images/gameIcons/".$items->getIcon($itemXML).".png\" alt=\"Item icon\" align=\"top\" />";
}
count++;
}
}

How to format a table to display images in PHP

I have images in an array and I want them to be display in a table with 3 columns!
So obviously in HTML, it is like:
<table>
<tr>
<td><img src="image1.jpg"></td>
<td><img src="image2.jpg"></td>
<td><img src="image3.jpg"></td>
</tr>
<!-- and so on... -->
</table>
So I want it to print out in PHP but the problem is the code.
My code is like this:
$photos = array( "image1",
"image2",
"image3",
"image4",
"image5",
);
foreach ($photos as $photo) {
$i = 0;
if ($i < 3) {
echo '<td><center><img src="'.$photo.'.jpg"></td>';
$i++;
} elseif ($i == 3) {
echo '</tr><tr>';
$i = 0;
}
}
I don't know what seems to be the problem and every time I echo out the variable $i, it echoes out like 11111111111111111111111111111111111111111111.
change this
foreach ($photos as $photo) {
$i = 0;
to
$i = 0;
foreach ($photos as $photo) {
Why is $i inside the foreach loop? That must be the reason. The $i gets initialized at the start of each loop cycle.
Try display:inline-block; instead in css. This works very well.
Here's an example...
<img src="image1.jpg" style="display:inline-block;>
<img src="image2.jpg" style="display:inline-block;>
<img src="image3.jpg" style="display:inline-block;>
You can even ask if you want to add text or something with the images...
I find using array_chunk for this kind of thing is more elegant.
$chunks = array_chunk($photos, 3);
foreach ($chunks as $chunk)
{
echo '<tr>';
foreach ($chunk as $photo)
{
echo '<td><img src="', $photo, '.jpg" /></td>';
}
echo '</tr>';
}
Also, don't use the 'center' tag, it is deprecated. Use CSS to align content.
Also, strictly speaking, this is not what HTML tables are for. Sure they work well. Actually, they work better than the 'proper' solution but they are not correct. Still, go ahead and use them if you just need a quick fix. It doesn't actually cause any problem to do so.

Categories