Jump to content

possibly storing values in a variable?


ash992

Recommended Posts

Hey guys, So I posted earlier and someone managed to help me with my problem, but my problem has slightly changed.. See basically I'm trying to create something that takes all values from a column called "UID", but only were the "Type" Column is set to the value "AD", and then choose one of them at random and echo an image with that number.

 

Here's how far I got

 

<?php

include ("connect.php");

$query = "SELECT MIN(UID), MAX(UID) FROM Users WHERE Type='AD'"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo '<img src="' . rand($row['MIN(UID)'], $row ['MAX(UID)']) . '.png>" <br />';

}

?>

 

Now that fully works however the problem is that despite getting the highest and lowest values, this means it will also getting all value in between, meaning some of them will not have the type AD, so I need to somehow figure out a way to just select the ones with the type AD and then pick one of them at random, Now I've done a lot of googling but I really can't figure out how I'm going to do this... Any help at all would be much appreciated! Thank-you in advance guys!

 

Ps. I also don't think I need the while command.. But I'm not quite sure how I'd do that without it... Thanks again guys!

Link to comment
Share on other sites

I think this is what you're asking for. Try it and see.

 

<?php
include ("connect.php");
$query = "SELECT UID FROM Users WHERE Type='AD'"; 
$result = mysql_query($query) or die(mysql_error());
while( $row = mysql_fetch_row($result) ) {
$array[] = $row[0];
}
$rand = array_rand($array);
echo "<img src=\"$array[$rand].png>\"<br />\n";
?>

Link to comment
Share on other sites

While that solution will work, I wonder how it will scale. You will be processing a lot of data from the database and in PHP that you don't really need. As the number of entries in the table increases, the process will take longer. What about this:

 

<?php
include ("connect.php");
$query = "SELECT MIN(UID), MAX(UID) FROM Users WHERE Type='AD'"; 
$result = mysql_query($query) or die(mysql_error());
# ADD THIS
$row = mysql_fetch_array($result)
$min = $row['MIN(UID)'];
$max = $row ['MAX(UID)'];
$rnd = rand($min, $max);

$query = "SELECT UID FROM Users WHERE Type = 'AD' AND UID >= $rnd ORDER BY UID LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result)
$TheRandomID = $row['UID'];
# REMOVE THE WHILE (Since we only have/need one row)
#while($row = mysql_fetch_array($result)){
echo '<img src="' . $TheRandomID . '.png>" <br />';
#}
?>

 

The second query will fetch the generated random ID or the next greatest UID (of type "AD"). Since we know that MAX(UID) is a valid selection, and rand() will not return anything greater, the query should always return a valid ID.

 

It is one extra trip to the database, but we reduced the overall amount of data being transferred and processed.

 

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.