Jump to content

Looping Question


avo

Recommended Posts

Hi All

Can anyone please help me with this puzzle

my code is

[code]<?
//ASSIGN VARIABLE FOR DROP DOWN BOX IN FOR LOOP
$num_rows=$_POST['list_in'];
//IF SELECT BUTTON PRESSED
if ($_POST['select_in']){
            mysql_connect ($dbhost, $dbuser, $dbpass);
            mysql_select_db ($dbname) or die ( mysql_error ());
            $query ="SELECT * FROM booking_in";
            $result =mysql_query ($query) or die (mysql_error());
            $count=mysql_num_rows($result);//COUNT ROWS IN DB

for($loop=0;$loop<$num_rows;$loop++){

            $row=mysql_fetch_array($result);
            $counter=$loop+1;//ASSIGN COUNTER VALUE
            $prt_num=$row['b_prt_number'];
            $prt_date=$row['b_date'];
            $prt_qty=$row['b_quantity'];
            $prt_tam=$row['b_tam'];
?>[/code]

$num_rows=$_POST['list_in']; this is a drop down box asigning a number to $num_rows
$count is the number of rows in my db

the above code works when i select a number from the drop down box the html (not visable) under the last code is echoed how evermany times

but lets say there is 5 rows in my db
and i use the dropdown box and select 6 the last record echoed is blank which i would expect
i would like to use the count to limit this if theres only 5 in the db but i select six to only echo 5

how can i achive this please
Link to comment
Share on other sites

[!--quoteo(post=386624:date=Jun 21 2006, 11:49 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 21 2006, 11:49 PM) [snapback]386624[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Change your for() loop to...
[code]
for($loop = 0; $loop < $num_rows, $loop < $count; $loop++) {
[/code]
Ive added spacing to make it clearer whats happening, its not really required.
[/quote]


Hi thanks

tryed that but when i enter 1(or upto rows in db) in the drop down box it displayes all in my db utill i go over the amount of rows in my db

if this makes sense
any ideas

thanks.
Link to comment
Share on other sites

[!--quoteo(post=386642:date=Jun 22 2006, 12:44 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 22 2006, 12:44 AM) [snapback]386642[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Maybe switch the order?
[code]
for($loop = 0; $loop < $count, $loop < $num_rows; $loop++) {
[/code]
[/quote]
Hi Thanks

Still had struggled with this for loop been looking at some functions see what can so the job came up with this

[code]for($loop=0;$loop<min($count,$num_rows);$loop++){[/code]

min(return lowest numeric value)

works good

cheers
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.