Jump to content

foreach loop question


ballhogjoni

Recommended Posts

I dont know the logic i shoudl use for this. Say I have queried the db and I have 4 results, I use mysql_fetch_array to put the results into an array and then I want to run a foreach to show the results. The tricky question is that I don't want to show the results in a normal table result IE

<table>
<tr><td>result 1</td></tr>
<tr><td>result 2</td></tr>
<tr><td>result 3</td></tr>
<tr><td>result 4</td></tr>
</table>

 

which will put each result into its own row and column. What I want to do is make the table look somthing like this:

<table>
<tr>
  <td>result 1</td><td>result 2</td>
</tr>
<tr>
  <td>result 3</td><td>result 4</td>
</tr>
</table>

 

How would I go about doing that? I hope this made sense.

 

Thanks

Link to comment
Share on other sites

Easy. Assume an array of 10 elements:

<?php

// $data = 10-item array
$total = count($data)
$html = "<table>";
for($i = 1; $i++; $i <= $total) { 
    // Calculate $total ahead of time, otherwise it checks each loop
    // Also, note that we start at 1 instead of 0 to make things easier,
    // otherwise the first item is even (0)
    if($i % 2 = 1)  { // If odd
        $newHtml = "<tr><td>{$data[$i]}</td>";
    }
    else { // it's even
        $newHtml = "<td>{$data[$i]}</td></tr>";
    }
    $html .= $newHtml;
}
echo $html;

Link to comment
Share on other sites

Easy. Assume an array of 10 elements:

<?php

// $data = 10-item array
$total = count($data)
$html = "<table>";
for($i = 0; $i++; $i < $total) { // Calculate $total ahead of time, otherwise it checks each loop
    if($i % 2 = 1)  { // If odd
        $newHtml = "<tr><td>{$data[$i]}</td>";
    }
    else { // it's even
        $newHtml = "<td>{$data[$i]}</td></tr>";
    }
    $html .= $newHtml;
}
echo $html;

 

Your for-loop is backwards.  It should be:

for($i = 0; $i < $total; $i++)

Link to comment
Share on other sites

<?php

for($i = 1; $i <= $total; $i++) {
    // I'm not sure if switch accepts expressions. You may need to assign the switch variable beforehand
    switch($i % 3) {
    case 1: break; // = 1/3
    case 2: break; // = 2/3
    case 0: break; // = 3/3
    default: break;
    }
}

To do that, you'll need to use this for loop instead. Just fill the cases in with the necessary HTML

Link to comment
Share on other sites

By the way, if you're trying to make a product grid or something similar, you're better off just using floats and adjusting the containing element's width so that only 2, 3, etc. fit per row. That's also the preferred approach because the number per row has very little to do with the nature of the content itself. For that reason, you want to keep this out of your code and use CSS instead. If you still want to enforce a number of elements per row, only do one check ($i % $numberPerRow == 0) and add a class to that item. From your CSS, use "clear: left" and that item will always reset the row.

Link to comment
Share on other sites

Ok i am having major problems here. I can't seem to get this correct. I have 5 items to be placed into a table. Basically the table can have as many rows as needed but there can only be 3 columns in each row. I have 5 items so there should be 1 row with 3 items and 1 row with 2 items. I need to start the $i at 0 because $featured_golf is an array with integer indexes starting at 0. any help would be great!

$total = count($featured_golf);
for($i=0;$i<$total;$i++) {
    if($i % 3 == 1){
    	echo $i % 3;echo '<br>';
		$newHtml = '<tr>
		<td style="padding-bottom:10px;">
		<table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;">
			<tr>
				<td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span>
				</td>
				<td style="padding-left:10px;"><a href="'.$base_href.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td>
			</tr>
		</table>
		</td>
		<td style="padding-right:10px;"></td>';
    }elseif($i % 3 == 2){
        $newHtml = '<td style="padding-bottom:10px;">
		<table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;">
			<tr>
				<td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span>
				</td>
				<td style="padding-left:10px;"><a href="'.$base_href.'Golf-Courses/'.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td>
			</tr>
		</table>
		</td>
		<td style="padding-right:10px;"></td>';
    }elseif($i % 3 == 0){
        $newHtml = '<td style="padding-bottom:10px;">
		<table border="1" cellspacing="0" cellpadding="0" style="font-size:12px;font-family: Arial, Helvetica, sans-serif;padding:8px;">
			<tr>
				<td><a style="color:#000000;font-weight:bold;" href="'.$base_href.$featured_golf[$i]['short_name'].'"><u>'.$featured_golf[$i]['name'].' Golf Club</u></a>:<br /><span style="font-size:11px;line-height:150%;">'.$featured_golf[$i]['featured_description'].' <a href="'.$base_href.$featured_golf[$i]['short_name'].'">Click Here</a></span>
				</td>
				<td style="padding-left:10px;"><a href="'.$base_href.$featured_golf[$i]['short_name'].'"><img src="'.$featured_golf[$i]['image']['href'].'" border="0"/></a></td>
			</tr>
		</table>
		</td>
	</tr>';
    }
    $featured_golf_string .= $newHtml;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.