mrt003003 Posted May 9, 2011 Share Posted May 9, 2011 Hi there, I'd be ever so greatful if someone could help me please. I'm really trying to get my head around working with arrays and could really do with an example in a context i fully understand. I have made a test page for this and made a query that will search for appropriate records in my database: <?php $colname_Class = "3"; $colname_Ships = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Ships = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_Ships = sprintf("SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1'", GetSQLValueString($colname_Ships, "text"), GetSQLValueString($colname_Class, "int")); $Ships = mysql_query($query_Ships, $swb) or die(mysql_error()); $row_Ships = mysql_fetch_assoc($Ships); $totalRows_Ships = mysql_num_rows($Ships); echo $row_Ships['ShipID']; echo ' '; echo $row_Ships['ShipName']; echo ' '; echo $row_Ships['Class']; echo ' '; echo ' '; echo $totalRows_Ships = mysql_num_rows($Ships); ?> After i made the query i echo out the ShipID, ShipName and Class from the queey which displays: 5 New Ship 3 1 So you can see the ShipID = 5, the name of the Ship is New Ship, its Class is 3 and there is only 1 record found in the database that matches the search criteria. Lets say there are 10 records in my database... What i really need to do is make an array of the outputted results, determine the number of records and then to randomly choose 1 of the results from the array. Can you help me please?? If i can see an example in this context i feel i'd really learn something here. If you can help, please do because im well stuck. Thank you Tom Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/ Share on other sites More sharing options...
jonsjava Posted May 9, 2011 Share Posted May 9, 2011 the ugly method (coding in the reply box, not my IDE, so not tested): $count = 0; while ($row = mysql_fetch_assoc($res)){ $array[$count]['shipID'] = $row['shipID']; $array[$count]['ShipName'] = $row['ShipName']; $array[$count]['Class'] = $row['Class']; $count++; } $total = count($array); $random = rand(0,$total - 1); $random_ship = $array[$random]; ?> Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212747 Share on other sites More sharing options...
mrt003003 Posted May 9, 2011 Author Share Posted May 9, 2011 Thats brilliant thank you, So inorder to get it to output the random record what do i echo $random_ship; ?? Nothing is outputted at all you see. Ive added another record so i now have 2 and if i echo $total; that shows theres 2 ships in the array. Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212756 Share on other sites More sharing options...
jonsjava Posted May 9, 2011 Share Posted May 9, 2011 try print_r($random_ship); Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212757 Share on other sites More sharing options...
mrt003003 Posted May 9, 2011 Author Share Posted May 9, 2011 Yes Yes Yes! Thats its oh wow thank you so much! Can i ask somethink else regarding this please??? With the random Ship thats outputted how would i turn it into a hyperlink and parse the recordID of the ship it has randomly selected?? I'm fine parsing usually but because its in the array im not sure how that would work?? Any ideas?? Thank you so much jonsjava! Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212759 Share on other sites More sharing options...
jonsjava Posted May 9, 2011 Share Posted May 9, 2011 ok, try this: $shipID = $random_ship['shipID']; $url = "http://some_site/ship_info.php?id=$shipID"; echo $url; Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212763 Share on other sites More sharing options...
mrt003003 Posted May 9, 2011 Author Share Posted May 9, 2011 Yes that outputs the url and the ID to be parsed. Its not in a hyperlink though so can i assume i could edit the $url to something like: $url = '< ahref =http://some_site/ship_info.php?id=$shipID>'Name</a>; Ive given it ago and it outputs Name, but its not linked. Thank you Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212767 Share on other sites More sharing options...
jonsjava Posted May 9, 2011 Share Posted May 9, 2011 That was an example to show you how to manipulate the data. Have fun with it! You are working with arrays now. Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212771 Share on other sites More sharing options...
mrt003003 Posted May 9, 2011 Author Share Posted May 9, 2011 Thanks again jonsjava ive learnt alot! Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212772 Share on other sites More sharing options...
mrt003003 Posted May 9, 2011 Author Share Posted May 9, 2011 Working link: $url = '<a href="http://some_site/ship_info.php?id='.$ShipID.'">Test</a>'; Link to comment https://forums.phpfreaks.com/topic/235917-create-array-and-randomly-choose-row/#findComment-1212781 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.