Web Hosting Talk







View Full Version : PHP Array manipulation problem


Aeron
10-13-2009, 11:08 AM
Hi guys,
I'm new here and I have a problem that I would really appreciate some help with.

What I need to to is pull some data from a mySQL database using the following code:


$sql = 'SELECT * FROM link_blog ORDER BY id DESC LIMIT 25';
$query = @mysql_query($sql);


I then need to be able to echo out 5 results, then break for some HTML code, and be able to break back into PHP and echo out the next 5 results in the array, and so on.

I normally use a while loop to return my values, but I need to be able to limit it to the first 5 results in the array, and then come back into PHP and pick up where I left off with the next set of 5 results in the array, and repeat.

To give you a code example of what I'm looking to do:

<?
//echo the first 5 results (needs counting or limiting code added)
while($array = mysql_fetch_array($query)){
echo'<a href="' . $array['url'] . '">' . $array['title'] . '</a>';
}
?>

//some html code

<?
//echo the next 5 results (needs counting or limiting code added)
while($array = mysql_fetch_array($query)){
echo'<a href="' . $array['url'] . '">' . $array['title'] . '</a>';
}
?>

//more html code. This needs to be repeated a number of times, which the pointer in the array moving back 5 each time.



If anyone knows how to easily do this, even if it's through a clumsy method like numerous mySQL queries, I'd be very appreciative.

Thanks for your help.

aniketh
10-13-2009, 11:15 AM
why not use a for loop ($i) over the record count and then if ($i>0 && ($i%5 ==0)) then echo the html you need

snapinpayments
10-13-2009, 11:25 AM
How about this


<?
//echo the first 5 results (needs counting or limiting code added)
while($array = mysql_fetch_array($query)){
$cnt++;
echo'<a href="' . $array['url'] . '">' . $array['title'] . '</a>';

if($cnt == 5){
echo "some html code";
//reset cnt
$cnt = 0;
}

}
?>

mwatkins
10-13-2009, 01:14 PM
+1 for aniketh's solution. Doing something ever N rows cries out for the use of the `mod` function/math (i.e. 20%5 == 0; 17%5 == 2). Very useful for coloring every other row in a table, or spitting out some alternate text every N rows as the OP wants to do.

Here's a Python example with output (too bad interactive use of PHP is so primitive):

>>> for n in range(20):
... if n % 5:
... print "some link"
... else:
... # output html you want every 5 links
... print "a navigation menu or somesuch"
...
a navigation menu or somesuch
some link
some link
some link
some link
a navigation menu or somesuch
some link
some link
some link
some link
a navigation menu or somesuch
some link
some link
some link
some link
a navigation menu or somesuch
some link
some link
some link

... you get the idea

Aeron
10-13-2009, 01:45 PM
Thanks for your help guys,
While I'm sure mwatkins and aniketh's solutions would have worked well, and are probably the more advanced way to solve the issue, I was able to get things working with a modification of snapinpayments' solution.

This didn't need to be very dynamic, and I was willing to hard-code it in if necessary.

The solution I used is as follows:


$sql = 'SELECT * FROM link_blog ORDER BY id DESC LIMIT 25';

$query = @mysql_query($sql);

while($array = mysql_fetch_array($query)){
$count++;
echo'<a href="' . $array['url'] . '">' . strtoupper($array['title']) . '</a>';

if($count == 5){
echo 'first unique html snippet';
}//end if

if($count == 10){
echo 'second unique html snippet';
}//end if

if($count == 15){
echo 'third unique html snippet';
}//end if

if($count == 20){
echo 'fourth unique html snippet';
}//end if

}//end while


Thanks again for your help guys, it's greatly appreciated.

Website themes
10-13-2009, 03:58 PM
Here is one that can be modified more easily. Just put your unique html snippets in the $html_snippet array :).


$sql = 'SELECT * FROM link_blog ORDER BY id DESC LIMIT 25';

$query = @mysql_query($sql);

$html_snippet=array();
$html_snippet[1]="First <html> snippet";
$html_snippet[2]="second html thingamajic";
$html_snippet[3]="third";


while($array = mysql_fetch_array($query)){
$count++;
echo'<a href="' . $array['url'] . '">' . strtoupper($array['title']) . '</a>';

if($count%5 == 0 ){
echo $html_snippet[$count/5];
}
}//end while