BrainCrasher Posted January 15, 2012 Share Posted January 15, 2012 Hello forum. I'm new here, but I've been reading and finding useful things for a while now. I'm still new to PHP and I need a little help. I'm doing a school project and I have some things I want to do, but do not know how to write it down in PHP. I think I'll ask a lot of questions this week, and I hope I will get some help.. For start I want to ask this: I've been using mysql_fetch_array() for doing loops and populating check-boxes. And everything's working fine.. but what I want is to control the actual loop by clicking buttons. Let's say first time a while-do is run, my check-boxes get populated from the database and every other loop the next data from the table is added.. pretty straightforward. I want to be able to populate once, then click "next" and the new data to be added and so on.. <?php $tema = mysql_query("SELECT * from questions")or die(mysql_error()); function answer1($string) { $string1 = explode("/", $string); echo $string1[0]; } function answer2($string) { $string1 = explode("/", $string); echo $string1[1]; } while ($row=mysql_fetch_array($tema)) { echo mysql_fetch_array($tema); $tip=$row["tip"]; if ($tip==2) { $id=$row['prasanje_id']; $question=$row['question']; $answer=$row['answer']; ?> <label> <?php echo $question?></label><br> <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0" /> <?php answer1($tekst) ?></label> <label> <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_1" /> <?php answer2($tekst) ?></label> <?php } } ?> I want an alternative to the while-do loop.. Is it possible to do this? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/ Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 not really sure what you are asking, but i will take a stab, perhaps you want to set a limit on the number or rows that the query grabs? Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307759 Share on other sites More sharing options...
BrainCrasher Posted January 15, 2012 Author Share Posted January 15, 2012 Not really. I want a way to manual loop through the table. If I have 10 rows, the code I wrote will get them all. question1 <checkbox> <checkbox> question2 <checkbox> <checkbox> question3 .. What I want is a "pausing effect" after each table row. question1 <checkbox> <checkbox> Pause. Click a button is what I want here to continue. question2 <checkbox> <checkbox> And so on.. Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307760 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2012 Share Posted January 15, 2012 Browsers and web servers operate using a HTTP request/response - http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol The browser makes a request for the page. The server outputs the requested page back to the browser. You must either output everything to the browser and incrementally reveal the content using client side code (javascript) or you must use separate HTTP requests/responses to get the information for each section of question/checkboxes. Since your desired output implies not refreshing the whole page to get and display the next section, you would use AJAX to make the separate HTTP requests and handle the response send back to the browsers to build and display each section in the browser. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307764 Share on other sites More sharing options...
wolfcry Posted January 15, 2012 Share Posted January 15, 2012 while-do That made me chuckle . It's actually called a "do-while" loop because the script will "do" something at least once and keep doing it it "while" a certain condition remains true. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307766 Share on other sites More sharing options...
SergeiSS Posted January 15, 2012 Share Posted January 15, 2012 while-do That made me chuckle. It's actually called a "do-while" loop because the script will "do" something at least once and keep doing it it "while" a certain condition remains true. You may do it of course - I mean 'That made me chuckle'. But in the code "while-do" loop is used and it's quite correct. It seems that you know only "do-while" loop and you've never heard about "whild-do" loop. "While-do" first of all checks a condition. If it's true it go inside and perform a code. It could happen that the code inside loop is not performed at all. As concerned "do-while" loop you are right - 'script will "do" something at least once' ©. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307797 Share on other sites More sharing options...
laffin Posted January 15, 2012 Share Posted January 15, 2012 sorry, it's called a while loop, or a do while loop. Wolfcry is correct. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307804 Share on other sites More sharing options...
SergeiSS Posted January 15, 2012 Share Posted January 15, 2012 sorry, it's called a while loop, or a do while loop. Wolfcry is correct. Maybe there is no difference when "normal" people (not programmers) are talking. For them there is no difference. But not for scripts! //If you do loop like this do { .... } while ( $i < 100 ); // and another loop while( $i < 100 ) { ... } You may get different results when use these two loops. Do you agree? If you say to a newbie "use do-while loop"... What do you think - what will he do? A bet he will use the first type of loop It doesn't matter if you mean the second type. He will use the first one. I'm talking about it because I saw this type of misunderstanding many-many times. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307811 Share on other sites More sharing options...
laffin Posted January 15, 2012 Share Posted January 15, 2012 Been coding for 30 yrs, never had that problem. use a for/while/do-while loop. And usually they get the loop structure correct, it's the expressions they usually get wrong. But now we are so way off topic What I want is a "pausing effect" after each table row. You will end up using javascript not php for this pausing/continuation effect Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307824 Share on other sites More sharing options...
BrainCrasher Posted January 15, 2012 Author Share Posted January 15, 2012 I know the difference between do-while and while. I was working in Pascal last semester so.. I got used to write while-do Can you at least give me a hint or some links of examples or anything of how to do this with JS? Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307846 Share on other sites More sharing options...
laffin Posted January 15, 2012 Share Posted January 15, 2012 Like this Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307848 Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 let's stay on topic, OP you will want to use a combination of mysql and ajax for this. depending on how exactly you want to go about it, you would query the first row to appear, then when the "next" button is clicked, send an ajax request to grab the next row, and so on. Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1307861 Share on other sites More sharing options...
BrainCrasher Posted January 16, 2012 Author Share Posted January 16, 2012 I've started learning the basics of AJAX.. because I've never used it. I'll see what I can do.. Thanks for pointing out! Quote Link to comment https://forums.phpfreaks.com/topic/255042-manual-loop-through-database-table/#findComment-1308197 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.