lopes_andre Posted October 20, 2008 Share Posted October 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/ Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669985 Share on other sites More sharing options...
revraz Posted October 20, 2008 Share Posted October 20, 2008 Each time you run through it, just update the counter by one. When you read it, just JOIN the tables and only display where the counter = id Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669987 Share on other sites More sharing options...
lopes_andre Posted October 20, 2008 Author Share Posted October 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669990 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 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'])); ?> Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-669995 Share on other sites More sharing options...
lopes_andre Posted October 20, 2008 Author Share Posted October 20, 2008 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é. Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-670013 Share on other sites More sharing options...
lopes_andre Posted October 20, 2008 Author Share Posted October 20, 2008 wrong post Quote Link to comment https://forums.phpfreaks.com/topic/129230-how-to-loop-phpmysql/#findComment-670083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.