Sydcomebak Posted August 14, 2008 Share Posted August 14, 2008 <?php $desired_num=4; $result = mysql_query("SELECT * FROM NamesTbl") OR die(mysql_error()); WHILE ($row = mysql_fetch_array($result) ) { echo $row($desired_num)[NameLast]; } ?> How do I display the 4th ($desired_num) value in the result? -Dave Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/ Share on other sites More sharing options...
widox Posted August 14, 2008 Share Posted August 14, 2008 Well, what exactly does $desired_num represent and how does the user/code pick it? Best bet would be to use that as criteria in your SQL statement. Such as SELECT * FROM NamesTbl WHERE id = SOMEID or somesuch where clause. Then you'll only need to retrieve 1 record instead of ALL records. Then you can simply display the results of your query and be done. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616833 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 I don't want to use a where statement. I need to be able to get all results and pick them whenever I need. I know it seems weird, but I have my reasons for using this odd way. I WAY oversimplified the code to show what I need. -Dave Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616840 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 <?php $desired_num=4; $result = mysql_query("SELECT * FROM NamesTbl LIMIT {$desired_num}, 1") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['NameLast']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616841 Share on other sites More sharing options...
akitchin Posted August 14, 2008 Share Posted August 14, 2008 or, if you wanted to grab the whole set and specify a row, look into mysql_data_seek(). however, selecting only the rows you need is always the best practice to reduce unnecessary overhead. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616846 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 OK, let's say that I have an array of 28 names and I want to display them in columns. I want to display NameLast[1] then NameLast[4] then Namelast[7] in the first row of a table. The easiest way to do that would be to do a for loop for x = 1 to 3 do begin echo NameLast[3x-2] end I hope that's clearer. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616855 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 <?php $desired_num=4; $result = mysql_query("SELECT * FROM NamesTbl") or die(mysql_error()); $x=0; while($row = mysql_fetch_array($result)) { $i=0; if($i % 3) echo $row['NameLast']; $i++; } //edited ?> Try that..edited Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616857 Share on other sites More sharing options...
Jabop Posted August 14, 2008 Share Posted August 14, 2008 In that instance I'd use mysql_data_seek(), as akitchin recommended. http://us2.php.net/mysql_data_seek $fourth = mysql_data_seek($result, 4); $seventh = mysql_data_seek($result, 7); Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616859 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 re-re-edited, sorry <?php $desired_num=4; $result = mysql_query("SELECT * FROM NamesTbl") or die(mysql_error()); $x=0; while($row = mysql_fetch_array($result)) { $i=0; if(($i % $desired_num) == 0) echo $row['NameLast']; $i++; } //edited ?> Try that..edited Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616861 Share on other sites More sharing options...
Jabop Posted August 14, 2008 Share Posted August 14, 2008 rofl90, that would only display 4, 8, 12, 16, 20, and so on. That's not what OP is looking for Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616865 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 Ooh... sorry I thought he was looking for every modulus of something. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616869 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 In that instance I'd use mysql_data_seek(), as akitchin recommended. http://us2.php.net/mysql_data_seek $fourth = mysql_data_seek($result, 4); $seventh = mysql_data_seek($result, 7); Doesn't this just send back a boolean saying that the row has data in it? Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616879 Share on other sites More sharing options...
akitchin Posted August 14, 2008 Share Posted August 14, 2008 it will. what you need to do is put an if() into the while() loop to tell it to jump to the row for the next iteration of the loop. keep in mind you haven't been totally clear - do you want to display them in three columns, or do you want to display them in columns of three? your 1, 4, 7 example only works if it keeps going (let's assume 28 names, the way you did): 1 4 7 10 13 16 19 22 25 28 2 5 8 11 14 17 20 23 26 3 6 9 12 15 18 21 24 27 Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616886 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 I'm building an array of results instead. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616897 Share on other sites More sharing options...
akitchin Posted August 14, 2008 Share Posted August 14, 2008 alright, but to be honest, you're wasting a fair bit of resources by not taking advantage of PHP's MySQL functions. it's roughly the same amount of coding work to get it working within the loop. i was going to write an example to work with, but i didn't know whether you'd be displaying in a set of 3 columns or columns of 3. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616898 Share on other sites More sharing options...
rofl90 Posted August 14, 2008 Share Posted August 14, 2008 Oh sorry I didn't totally understand, well I gave you the concept, you can figure out the easy bits. or what you want to do i guess then you need 2 nested for loops to do this start 1 2 3 4 middle 1 4 7 10 2 5 8 3 4 end 1 4 7 10 2 5 8 11 3 6 9 12 4 9 12 13 Using increments of one in your first loop, and increments of 3 in your nested loop. Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616899 Share on other sites More sharing options...
unkwntech Posted August 14, 2008 Share Posted August 14, 2008 If you know what order the rows are retuned and which item you want use mysql_result($result, $desired_num, 'columName) Quote Link to comment https://forums.phpfreaks.com/topic/119724-solved-displaying-the-nth-result/#findComment-616906 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.