Jump to content

Create array and randomly choose row


mrt003003

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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! :D

Link to comment
Share on other sites

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