PHP Looping script

This is a discussion on "PHP Looping script" within the PHP Forum section. This forum, and the thread "PHP Looping script are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 5th, 2007, 19:39
New Member
Join Date: Mar 2007
Location: London
Age: 26
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Looping script

Hello all
I'm new here and I need some advice. Im trying to create a script that will loop and return multiple variables.
for example I have a mysql database with image records in it. I want the script to return something like

$image1="image1name"
$image2="image2name"

but the amount of images in the database changes all the time so one day i may have 10 and the next day 25

I hope this makes sensei
Thanks in advance
Pete
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 5th, 2007, 20:54
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Looping script

Check out arrays. From what it sounds like, those will make your life a lot easier.

http://php.net/array
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 6th, 2007, 12:30
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Looping script

It would help if you gave a description of the table including a row or two of sample contents, and how it was populated.

I've never really done this, but here's an interesting idea at least, assuming 1) a table whose first field is the autonumber and 2) the second field is a graphic that you want identified with the autonumber:
Code: Select all
while($row = mysql_fetch_row($result)) {
    $no = $row[0];
    $a=image;
    ${$a . $no} = $row[1];
}
I don't have time to play with it, but this (or something along this line) might work, and would give $image1=(image in first row), $image2=(image in second row).

Just a thought. If I'm missing something really stupid, I'm sure someone will point it out
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Mar 10th, 2007, 00:04
Junior Member
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Looping script

Hi there

You can fairly easily create $image1 to $image100 etc., using variable variables, however given the number is going to shift ongoing it would be better to learn to use arrays and store the images in an array instead:
$images = array(); $counter = 0;
while ($r = mysql_fetch_row($result)) {
//you could say
$images[$counter] = $r[0]; // or wherever in the result set the image is
//or perhaps
array_push($images, $r[0]);
//also several other ways; if you really want to assign to strings
${"image$counter"} = $r[0];
//look up variable variables for more info on php.net
$counter++;
}
//this is what your array now has:
print_r($images);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
dynamic variables, looping script

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Button 'Looping' for Navigation starrzo JavaScript Forum 2 Apr 4th, 2008 14:38
Looping through arrays assgar Starting Out 1 Apr 22nd, 2007 18:43
Looping through objects with variables and strings [Help Please] andrewbriscoe87 JavaScript Forum 1 Mar 24th, 2007 16:14
looping issue with flash file snooper Flash & Multimedia Forum 9 Nov 20th, 2006 13:34
looping background music gwx03 Flash & Multimedia Forum 6 Sep 7th, 2003 03:07


All times are GMT. The time now is 15:49.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42