Dynadot โ€” .com Transfer

15NP$ to solve this problem.

Spaceship Spaceship
Watch
Impact
15
15NP$ to the first person who solves it.
$catagoriesList is an mysql_fetch_array from a result of a sql query.
Here's my problem, I'm trying to display a mysql table into two sections, but the following isn't working:
PHP:
	  $countCatagories = count($catagoriesList);
	  $countCatagoriesHalf = $countCatagories / 2;

	echo '<div id="category">';
    for($i=0;$i<$countCatagoriesHalf;$i++) {
    	foreach($catagoriesList as $catagoriesListItem) {
     	   echo '<p><a href="'.$catagoriesListItem['catid'].'/">'.$catagoriesListItem['catname'].'</a></p>';
        }
    }
    echo '</div>';
    echo '<div id="category">';
    for($i=$countCatagoriesHalf;$i<$countCatagories;$i++) {
    	foreach($catagoriesList as $catagoriesListItem) {
     	   echo '<p><a href="'.$catagoriesListItem['catid'].'/">'.$catagoriesListItem['catname'].'</a></p>';
        }
    	
    }
    echo '</div>';
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
What is the result of this? I'm guessing that it displays the list twice, am I right? One way to solve is to drop the foreach loops and use $catagoriesList[$i]['catid'] etc instead. Or you can run one foreach and check if it had reached the middle of the list and then print out the div tags.
 
0
•••
Hey snareklutz,

I played around with the code, and created my own fake set of sql results (variable is $catagoriesList).
Here's my code:
PHP:
<?php

// my fake results
$catagoriesList[]=array("catid"=>"1","catname"=>"category1");
$catagoriesList[]=array("catid"=>"2","catname"=>"category2");
$catagoriesList[]=array("catid"=>"3","catname"=>"category3");
$catagoriesList[]=array("catid"=>"4","catname"=>"category4");
$catagoriesList[]=array("catid"=>"5","catname"=>"category5");
//end my fake results

$countCatagories = count($catagoriesList);

$countCatagoriesHalf = ceil($countCatagories / 2);
echo '<div id="category">';

  for($i=0;$i<$countCatagoriesHalf;$i++) {
  echo '<p><a href="'.$catagoriesList[$i]['catid'].'/">'.$catagoriesList[$i]['catname'].'</a></p>';
  }

echo '</div>';
echo '<div id="category">';

  for($i=$countCatagoriesHalf;$i<$countCatagories;$i++) {
  echo '<p><a href="'.$catagoriesList[$i]['catid'].'/">'.$catagoriesList[$i]['catname'].'</a></p>';
  }

echo '</div>';

?>

And here's the result I got:
Code:
<div id="category"><p><a href="1/">category1</a></p><p><a href="2/">category2</a></p><p><a href="3/">category3</a></p></div><div id="category"><p><a href="4/">category4</a></p><p><a href="5/">category5</a></p></div>

You really did not need that foreach statement. And when you devided $countCatagories by 2, you did not account for odd numbers (so if there were five rows, the result would be 2.5, giving erronosous results). So i fixed that up for you using the ceil statement. So if you had 5 rows in total, 3 of the results would be for your first half, and the other 2 for your second. If you had 11 results, 6 would go to the first half, and 5 to the second... See?

Anyways, hope thats what you wanted :). If it works out rep is much appreciated!

Thanks,
Rhett.
 
Last edited:
1
•••
Thanks, sent NP$ + rep.
 
0
•••
0
•••
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back