Jump to content

How to Loop PHP/MYSQL


lopes_andre

Recommended Posts

TABLE_NAMES

id | Name

----------

1  | Peter

2  | John

3  | Maria

 

TABLE_COUNTER

counter

1

 

I'am trying to make a loop for each time I run the "show_me_the_name.php" the script shows me a name, using two tables "TABLE_NAMES" and "TABLE_COUNTER"

 

For example:

 

On the first time I run the php script shows "Peter";

Second time, "John";

3º time, "Maria";

4º time, "Peter";

5º time, "John";

 

How can I start for get this done?

Link to comment
Share on other sites

do you mean ever time you refresh the page it shows the next one?

 

also, is this on a per-person basis or global? so, if user 1 goes to the page, they get Peter. if user 2 then goes to it, should they start with Peter or go right to John?

Link to comment
Share on other sites

Hi,

 

The script must be global. If I run the script I get the name of id1, if another person run the script, that person will get the id2 name. The counter will do this counting "OLD.id + 1" each time the php script runs.

 

How can I start? There are some examples on how to do this loops using php and mysql?

Link to comment
Share on other sites

There won't be any loops....each time the page loads, i would run something like the following:

 

<?php
// Do db connection stuff here

//Get next record
$result = mysql_query("SELECT * FROM `TABLE_NAMES` WHERE `id` > (SELECT MAX(counter) FROM `TABLE_COUNTER`) LIMIT 1");
if(!$result){
  //handle for when it's maxed out
}
//Get the row (there will only be one)
$data = mysql_fetch_assoc($result);
//Print out the data for debugging
print_r($data);
//Update counter
mysql_query(sprintf("UPDATE `TABLE_COUNTER` SET `counter` = '%s'",$data['id']));

?>

Link to comment
Share on other sites

Hi rhodesa,

 

I have not yet tested the code. But thanks a lot!

 

My english is bad, but I was try to apply the word "loop" to this:

 

1º time, show id=1

2º time, show id=2

3º time, show id=3

4º time, show id=1

5º time, show id=2

 

I have only 3 names in the database and i want to loop forever.

 

Best regards,

André.

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.