Jump to content

Manual loop through database table


BrainCrasher

Recommended Posts

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!!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' ©.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.