chelnov63 Posted January 20, 2009 Share Posted January 20, 2009 I have the following code : $rs_brass = mysql_query("SELECT * FROM the_brass"); $row_brass = mysql_fetch_array($rs_brass); echo $row_brass["image"]; //this echos out [b]some.jpg[/b] however suppose i want to see the value of image of the 4th recordset..i tried: echo $row_brass["image"][4]; but this doesnt seem to do the trick... any feedback/help appreciated... thanks Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/ Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 array start from 0 not 1 example <?php $image=array("a.jpg","b.jpg","c.jpg","d.jpg"); echo " ".$image[3].""; ?> example 2 <?php $image=array("image1"=>"a.jpg","image2"=>"b.jpg","image3"=>"c.jpg","image4"=>"d.jpg"); echo " ".$image['image4'].""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741073 Share on other sites More sharing options...
chelnov63 Posted January 20, 2009 Author Share Posted January 20, 2009 thanks for your help.. I have 10 records in my database, so should'nt echo $row_brass["image"][1]; still show the value of the whats in the image column? it doesnt show it.. do u know the correct syntax for pointing to a record in the recordset? for e.g echo $row_brass["image"]; shows the value in the first row of the image column i.e some.jpg so shouldnt echo $row_brass["image"][1] show the value in the second row of the image column in the database? Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741077 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 $rs_brass = mysql_query("SELECT * FROM the_brass"); echo $row_brass[4]; //this echos out some.jpg Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741083 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 no loop needed. <?php $image=array("a.jpg","b.jpg","c.jpg","d.jpg"); echo " one i want >>>>". $image[3]; echo "<br>"; var_dump($image); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741085 Share on other sites More sharing options...
chelnov63 Posted January 20, 2009 Author Share Posted January 20, 2009 thanks for your help mate .. but you are hardcoding your array.. im trying to get the value from a recordset, so your method does'nt really aid me in this instance .. i just need to know how to point to a particular record in the recordset and retrieve the value of a particular column of that record.. echo $row_brass[4] doesnt give me any proper value as how will it know the column I am trying to target? there are 5 different columns... thanks for your help anyway... Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741090 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 here an example how the array acts in and out off a loop <?php $image=array("a.jpg","b.jpg","c.jpg","d.jpg"); foreach($image as $gotu){ echo " <br> in a loop but wrong result >>> {$gotu[3]} <br><br>"; echo " <br> <br> in a loop correct result>>> {$gotu} <br><br>"; } echo " <br> out off the loop {$image[3]}"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741095 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 this will get the array of number 4 but array start at 0 so array 3 is correct for the 4th info. $rs_brass = mysql_query("SELECT * FROM the_brass"); echo $row_brass[3]; //this echos out some.jpg sorry <?php $rs_brass = mysql_query("SELECT * FROM the_brass"); echo $rs_brass[3]; //this echos out some.jpg ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741097 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 safe way. <?php $image=array("a.jpg","b.jpg","c.jpg","d.jpg"); if(in_array("d.jpg",$image)){ $result=$image[3]; } echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741102 Share on other sites More sharing options...
chelnov63 Posted January 20, 2009 Author Share Posted January 20, 2009 thanks but i think we are on different tracks at the moment :-\ ... i know the concepts of arrays... if you are able to show me using this terminology and without creating your own array and without loops ( we should be able to target an element of an array directly without any loops, if we know the index of the array required) that would greatly help: $rs_brass = mysql_query("SELECT * FROM the_brass"); $row_brass = mysql_fetch_array($rs_brass); how do we target directly the 4th record and its value in the image column... thats all i need... theres a specific point on the page where i always just need to show the image of the fourth record... no other records will ever be added or deleted to the table... <img src="<?php echo "image from fourth record" ?>" /> $rs_brass[4] echos out some.jpg of the first record .... but then how do i echo it out for the second record? I tried $rs_brass[1][4] but that doesnt work... Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741104 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 in over words you dont use the loop , to get the result you want or your get the incorrect result. Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741105 Share on other sites More sharing options...
chelnov63 Posted January 20, 2009 Author Share Posted January 20, 2009 I could be wrong but i believe if i have an array of records and I know the index number of the record i want, then I should be able to retrieve it directly from the array... anyway thanks for your help dude..maybe we'll get some other replies which can help us further.. Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741107 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 look in the database you got the field as image, and there many image names under the name image. when you loop with your $rec['image']; you get all the info in the image database field. now if you needed to use one off them image names via the loop your need to call it via it name. so example <?php if($rec['image']=="a.jpg"){ //do somethink } ?> you can also use the array with no loop like this <?php $rs_brass = mysql_query("SELECT * FROM the_brass"); echo $rs_brass[3]; //this echos out some.jpg ?> That saying that array number 3 is the data you want, there no other way. becouse alll the images are under the database field of image so the loop makes you call it via it name but the array let you call it by it array position not using the loop. Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741114 Share on other sites More sharing options...
saint959 Posted January 20, 2009 Share Posted January 20, 2009 Hi, you could try this if you know which record it is specifically that you wanted: <?php $rs_brass = mysql_query("SELECT * FROM the_brass ORDER BY `key` LIMIT 3,1"); $row_brass = mysql_fetch_array($rs_brass); echo $row_brass["image"] ?> that would then get the 3rd record Hope that helps / works Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-741130 Share on other sites More sharing options...
chelnov63 Posted January 21, 2009 Author Share Posted January 21, 2009 thanks for all your help guys ..appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-742457 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 Hi, you could try this if you know which record it is specifically that you wanted: $rs_brass = mysql_query("SELECT * FROM the_brass ORDER BY `key` LIMIT 3,1"); $row_brass = mysql_fetch_array($rs_brass); echo $row_brass["image"] ?> that would then get the 3rd record Hope that helps / works So then he would have to do a query every time he wanted to retrieve a record from the database. Just use a multi-dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-742468 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 OR, if you want to keep all of the data, <?php $rs_brass = mysql_query("SELECT * FROM the_brass"); // do a loop to gather all results for($i=0;$row_brass = mysql_fetch_array($rs_brass);$i++) { // save data into large array $array[$i] = $row_brass["image"]; // If it is row 3 (item #4, remember it starts with 0) if($i == 3) echo $row_brass['image']; } print_r($array); // show full array ?> Quote Link to comment https://forums.phpfreaks.com/topic/141583-point-to-a-particulatr-record-in-a-recordset/#findComment-742469 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.