Jump to content


Photo

Looping Question


  • Please log in to reply
4 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 21 June 2006 - 10:37 PM

Hi All

Can anyone please help me with this puzzle

my code is

<?
//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'];
?>

$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
Im loving it ........

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 June 2006 - 10:49 PM

Change your for() loop to...
for($loop = 0; $loop < $num_rows, $loop < $count; $loop++) {
Ive added spacing to make it clearer whats happening, its not really required.

#3 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 21 June 2006 - 11:04 PM

[!--quoteo(post=386624:date=Jun 21 2006, 11:49 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 21 2006, 11:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change your for() loop to...
for($loop = 0; $loop < $num_rows, $loop < $count; $loop++) {
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.
Im loving it ........

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 June 2006 - 11:44 PM

Maybe switch the order?
for($loop = 0; $loop < $count, $loop < $num_rows; $loop++) {


#5 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 22 June 2006 - 06:19 AM

[!--quoteo(post=386642:date=Jun 22 2006, 12:44 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 22 2006, 12:44 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Maybe switch the order?
for($loop = 0; $loop < $count, $loop < $num_rows; $loop++) {
[/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

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

min(return lowest numeric value)

works good

cheers
Im loving it ........




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users