SQL Result Random loop through - php

I have a SQL database that contains a field. I can get all of the elements in that field by:
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$ftype =$row['ftype']; //name of field is 'ftype'
print $ftype} //do something
I want a random element to be printed out each time the page is opened/refreshed.
Knowing that the result is an array containing the information i want, I want to randomly choose an element of the array. Also, if there are N elements in the array, I want to choose all N elements exactly once before I show any element again for second time. The process repeats itself. I know how to code something like this in java or python, but I don't think that's the way I should go.
I think I need to use javascript, but I'm just not sure what technology to use.
UPDATE:
Patrick's idea seems to be exactly what i was looking for. I thought I would share what I have now and maybe you can suggest optimization. I hope the intent in the code is obvious.
<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
$count = 0;
$randomArray = array();
$sql="SELECT youtubeurl FROM Foodlist";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
array_push($randomArray,$row['youtubeurl']);
}
shuffle($randomArray);
$_SESSION['randomArray'] = $randomArray;
$_SESSION['count'] = $count;
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
$_SESSION['count'] = 0;
$randomArray = $_SESSION['randomArray'];
shuffle($randomArray);
$_SESSION['randomArray'] = $randomArray;
} else{
$randomArray = $_SESSION['randomArray'];
$count = $_SESSION['count'];
echo $randomArray[$count];
$_SESSION['count']++;
}
?>

You can do this without Javascript, however you'll need to open/maintain a session.
Pseudocode:
data = data_from_mysql()
choice = random.choice(data, exclude = SESSION['choices'])
SESSION['choices'].append(choice)
print choice
if len(SESSION['choices']) == len(data):
SESSION['choices'] = []

If you want a random result let the dbms take care of it. Add this to the bottom of your sql query.
ORDER BY RAND()
LIMIT 1

Related

Using while() and continue in PHP

I am learning PHP and trying to use the while and continue expressions correctly.
I have a script that creates a 6 digit PIN, and I want to ensure that it is unique, otherwise I want to generate another PIN.
while(1) {
$pin = rand(111111,999999);
$sel = mysql_query("SELECT * FROM formusers WHERE pin = '$pin'");
if(mysql_num_rows($sel) != 0) { continue; }
mysql_query("INSERT INTO formusers(email,password,pin) VALUES('".$_POST['srEmail']."','".$_POST['srPass']."','".$pin."')");
if(mysql_affected_rows()!=-1) {
echo "Pin:" . $pin;
exit;
} else {
echo "Existing email, try again<br />";
}
break;
}
Do I have the syntax for the loop correct? It seems to work, but there's no way for me to debug it in the instance the rand() function creates the same PIN twice.
Also, in the event I did run out of unique PINs, what would happen here? Presumably it would loop indefinitely? Is there any way to prevent this?
Yeah, the code works, but as stated the infinite loop is concerning. You could possibly solve that like this:
var $i = 0;
while($i < 10) {
$i++;
$pin = rand(111111,999999);
$sel = mysql_query("SELECT * FROM formusers WHERE pin = '$pin'");
if(mysql_num_rows($sel) != 0) { continue; }
mysql_query("INSERT INTO formusers(email,password,pin) VALUES('".$_POST['srEmail']."','".$_POST['srPass']."','".$pin."')");
if(mysql_affected_rows()!=-1) {
echo "Pin:" . $pin;
exit;
} else {
echo "Existing email, try again<br />";
}
break;
}
This would ensure you would never make more than 10 iterations. However, the larger problem is probably that even that sampling size won't work in the future. This presents a bit of a conundrum. One possible approach is to determine how many unique numbers exist before starting the loop by counting them and then iterate that many times, but that approach is something like n2 (maybe, I'm not real good on big-O, I just know it would be bad).
You can leverage the unique nature of php array keys for this.
function getPins($qty){
$pins = array();
while (count($pins) < $qty){
$pins[rand(111111,999999)] = "";
}
return array_keys($pins);
}
This will make sure that for a given run you will get $qty number of unique pins. You might want to look at appending a unique prefix to each run of the function to avoid multiple runs creating collisions between the two.
You can seed random, so it gives you the same values every time, you run it:
srand (55 ); //Or some other value.
The while loop syntax looks okay to me.

Running a statement based on information from POST variable

I'm in a bit of pickle but the answer is probably pretty simple.
So I have my POST variable:
Array ( [accept] => accept,29 [accept1] => accept,30 [submit] => Save Selections )
That's just a print_r of $_POST.
Basically what I want to do is get the first variable, remove the 'accept,' part and store the number next to it in a variable, run some MySQL queries using that variable (containing that number) then move onto the next one; removing the 'accept,' storing the number next to it and running a query using that stored number. It want to do this for as many times is necessary to go through all the elements containing 'accept,' then a number.
Any help would be appreciated.
I was playing around with some ideas and have this code. It obviously doesn't work but perhaps I could fix and build on it?
while($i <= $elements)
{
while($x == 1)
{
$id = explode(',', next($_POST));
echo $id;
$x = 0;
}
$i++;
}
Im not sure this would fix your problem but I hope this helps and gives you an idea:
$i = 0;
while($i <= count($your_array))
{
$your_var = substr($your_array[$i], 7, 2); //get the number from your accept,29
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
UPDATE - Im not sure if this would work
foreach($your_post_array as $items)
{
$your_var = substr($items, 7, 2);
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
Maybe you should try to echo first the variables to make sure echo $your_var = substr($items, 7, 2);
I would use a foreach loop to loop through $_POST, check if the key starts with accept (stripos?), explode the value if it does and get the second value of the found array and store that somewhere for later use.
By the way, if possible I would change the front-end so that you have just one variable (an array) in $_POST you have to loop through.

Storing mysql result in array (without echoing) and then using that array to set another variable

I've tried looking for a solution in other questions asked before (as always), but I can't seem to wrap my head around this.
See, I want to get a number of unique IDs (in random order) from one table and store them in an array without echoing them. Then I want to use that array variable in a loop, so that I can increment the key with every pass, and set another variable to that array variable. Confusing? I think looking at the code will make it more clear.
The problem is I can't seem to store the values that I've queried into an array for later use in the code. I pasted the pertinent part of the code with my spots of trouble indicated by comment /* */ tags.
Any help is appreciated.
<?php
include ('parse_functions.php');
if ($fetch['use_rand']=='yes')
{ $loop = 5;
$concept = $fetch['concept'];
$countRandom = "SELECT exID FROM examples WHERE concept='$concept' ORDER BY RAND()";
$askForRandom = mysql_query($countRandom) or die(mysql_error());
/* HERE I NEED TO STORE RANDOM KEYS (exID) INTO AN ARRAY */ }
else
{ if (!empty($fetch['ex5'])) { $loop = 5; }
elseif (!empty($fetch['ex4'])) { $loop = 4; }
elseif (!empty($fetch['ex3'])) { $loop = 3; }
elseif (!empty($fetch['ex2'])) { $loop = 2; }
elseif (!empty($fetch['ex1'])) { $loop = 1; }
else { $loop = 0; }
}
if ($loop!==0)
{
echo '<div id="examples">' . "\n";
echo '<table class="showExample" cellspacing="0" cellpadding="0" border="0" align="center">' . "\n";
$turns = 1;
do {
if ($fetch['use_rand']=='no')
{ $exID = $fetch['ex'.$turns.'']; }
else
{ $exID = /* THIS IS WHERE I WILL USE "RANDOM VARIABLE" */; }
$askExamples = "SELECT * FROM examples WHERE exID='$exID'";
$getExamples = mysql_query($askExamples) or die(mysql_error());
$sortExamples = mysql_fetch_assoc($getExamples);
echo '<tr>' . "\n";
// ...and so on
If all you need to do is store the info in an array, here's an easy way about it.
/* code
to open
db here
*/
/* assuming you have a value for $concept */
$countRandom = "SELECT exID FROM examples WHERE concept='$concept' ORDER BY RAND()";
$askForRandom = mysql_query($countRandom) or die(mysql_error());
$Element = 0; //Array elements start at ZERO. So this is to intialise it.
while ($Fields = mysql_fetch_array($askforRandom)) //As long as there are records, get them.
{
//Records are retrieved one at a time. So store each one's exID in the array element
$Data[$Element] = $Fields["exID"];
//Soon after storing one, increment the value of the $Element variable so it is ready for the next one.
$Element++
}
/* now you have the data in the array. So you can do what you like with it. */
hope it helps
NOTE: when reading the array, make sure you put the condition to check that your counter is LESS than $Element. This is because after reading the last record, $Element is incremented by one. Hope this is clear.
Why not just grab all the right information in one query from the start along the lines of something like this:
select
a.exID,
examples.someField,
examples.someOtherField
from
examples
join
(
SELECT
exID
FROM
examples
WHERE
concept='$concept'
ORDER BY
RAND()
limit 5
) a
on a.exID=examples.exID
Then you just just pop them into an array (or better yet object) that has all the pertinent information in one row each time.

External looping of the variable

How could I best loop the data through a variable that's nested inside a 'while' loop but it's called outside of it ? Like in this example:
PHP:
$fr_q2 = mysqli_query($connect,"SELECT * FROM friends WHERE username ='".$_SESSION['user']."'
ORDER BY id DESC");
while ($rowsPicFr2 = mysqli_fetch_array($fr_q2)) {
$friends_q2[] = $rowsPicFr2['added_friend'];
$frn[] = $rowsPicFr2['added_friend'];
$frn2 = $rowsPicFr2['added_friend'];
}
$rowscheck = mysqli_num_rows($fr_q2);
for ($i=0; $i<$rowscheck; $i++)
HTML:
YES
So I need to pass $frn[$i] into a remdata() function - but the $frn[$i] needs to loop....All I get is a string of all ids from 'friend_added' in $frn[$i]....Thanks.
Once this HTML is sent to the browser, it won't loop anymore. It will just be HTML. So what you need to do, is either have it loop in JavaScript or simply echo each remdata():
YES
This solution is only really an option if you have very few elements in $frn.
Your question is confusing; I think you need the loop for the HTML output
<?php foreach ($frn as $i => $friend_id){ ?>
YES
<?php } ?>
Based on your comment in toon81's answer, it seems like you're having a problem dealing with several loops in your output that deal with the same result set in your database query. I'm not sure. I'd suggest in the future that you try to make your question easier to follow. For instance, do we need to know it's a social networking app? Your variable names aren't inherently easy to understand; what's the difference between $frn and $frn2? Presumably that's 'friend', but I keep reading it as 'fern'. You also only provided one line of your output, but your problem seems related to it's interaction with other output. Your code is cut off -- the rowscheck loop doesn't have a definition.
That said, this is a high level suggestion of how I'd handle your work differently. Data preparation:
$connection = ...;
$user = $_SESSION['user'];
$sql = "
SELECT added_friend
FROM friends
WHERE username = '$user'
ORDER BY id DESC
";
$response = mysql_query($sql, $connection);
$added_friends = array();
while ($row = mysql_fetch_object($response)){
$added_friends[] = $row->added_friend;
}
Ouput handling:
// With one loop if the markup can be ouput all at once.
foreach ($added_friends as $friend){
// Your 'friends_q2', whatever that is.
echo "Friends_q2: $friend";
// Your 'frn2' output, whatever that is.
echo "Frn2: $friend";
// Your 'frn' output.
echo "YES";
}
// ...or multiple loops if it can't.
foreach ($added_friends as $friend){
// Your 'friends_q2', whatever that is.
echo "Friends_q2: $friend";
}
foreach ($added_friends as $friend){
// Your 'frn2' output, whatever that is.
echo "Frn2: $friend";
}
foreach ($added_friends as $friend){
// Your 'frn' output.
echo "YES";
}
In any case, you're handling the same ID three times in different lists and in different ways. I'm not at all sure why. Is this what you're asking for help with?

When listing information from a database using php and mysql how would you make the first row look different to the rest?

Basically I have articles in my database and I want to alter the way the first record displays. I want the lastest (Posted) article to be the focus and the older article just to list, (see F1.com). I need to know how to get the first of my values in the array and get it to display differently but I am not sure how to do this, I can do it so all rows display the same just not how to alter the first row. I also need to know how to tell the rest of the rows to display the same afterwards im guessing you use an if statement there and before that some kind of count for the rows.
Current code:
$result = mysql_query("SELECT * FROM dbArticle WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result))
{
echo "<h2 class=\"heading1\">". $row['title'] ."</h2>";
echo "By: ".$row['username']." Type: ".$row['type']." Posted: ".$row['timestamp']."
$body = $row['body'];
echo "<br/><p>";
echo substr("$body",0,260);
echo "...<span class=\"tool\"><a class=\"blue\" href=\"index.php?pageContent=readArticle&id=".$row['id']."\">Read More</a></span></p><hr/>";
}
mysql_close($con);
Ok I have taken Luke Dennis's code and tried to test it, but I am getting this error: Warning: Invalid argument supplied for foreach() this is the line of the foreach statment. Something that has just come to mind is that I will only want 5 or so of the older articles to display. This is what I have thats creating the error:
<? $con = mysql_connect("localhost","****","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("******", $con);
$result = mysql_query("SELECT * FROM dbArticle ORDER BY timestamp DESC");
$first = true;
foreach($result as $row){
if($first)
{
echo"".$row['title']."";
echo"this is the headline";
$first = false;
}
else
{
echo"".$row['title']."";
}
}
?>
Do I need to add mysql_fetch_array somewhere to set the array up?
I would just iterate through the results and apply a css class to the first entry:
$first = true;
while ($row = mysql_fetch_assoc($result)) {
$cssClass = '';
if ($first) {
$cssClass = 'highlight';
}
echo '<p class="' . $cssClass . '">' . $row['text'] . '</p>';
$first = false;
}
It's a bit crude, but I often hard-code a variable to designate the first run through a loop. So something like:
$first = true;
foreach($list_of_items as $item)
{
if($first)
{
// Do some stuff
$first = false;
}
else
{
// Do some other stuff
}
}
A simple if statement when looping through your results will usually do the trick. You can use a boolean to indicate if you've output the first row of results or now. If you haven't then give it a particular style and then set the boolean to true. Then all subsequent rows get a different style.
All of the above are correct. Luke Dennis' post is of course fleshed-out a bit more.
As Brian Fisher said, add some CSS styling to the first link when you encounter it per Luke's post.
I took a look at the article list on the F1 website. Pretty well constructed site - "One would expect that." :-)
Anyway, the article listings are contained within a two row table (summary="Latest Headlines") in descending order (newest first).
Just place a class in the second column (<td class="first-news-article">). Then add the class name and appropriate styling values in the css file - probably your' modules.css. There's already quite a few class values associated with articles in that file, so you may be able to just use an existing value.
That should be about it - other than actually doing it!
By the way, judging by the quality of the underlying html, I'm assuming there's already an "article list emitter." Just find that emitter and place the appropriate conditional to test for the first record.
Darrell
I just noted your code addition. I assume that you were showing the F1 site as an example. Anyway, I think you're on your way.
I presume you have some code that loops through your resultset and prints them into the page? Could you paste this code in, and that might give us a starting point to help you.
I don't know PHP, so I'll pseudocode it in Perl. I wouldn't do it like this:
my $row_num = 0;
for my $row ($query->next) {
$row_num++;
if( $row_num == 1 ) {
...format the first row...
}
else {
...format everything else...
}
}
The if statement inside the loop unnecessarily clutters the loop logic. It's not a performance issue, it's a code readability and maintainability issue. That sort of thing just BEGS for a bug. Take advantage of the fact that it's the first thing in the array. It's two different things, do them in two different pieces of code.
my $first = $query->next;
...format $first...
for my $row ($query->next) {
...format the row...
}
Of course, you must make the first row stand out by using tags.
I'd use array_shift():
$result = mysql_fetch_assoc($resultFromSql); // <- edit
$first = array_shift($result);
echo '<h1>'.$first['title'].'</h1>';
foreach ($result as $row) {
echo '<h2>'.$row['title'].'</h2>';
}
The best way to do this is to put a fetch statement prior to the while loop.
Putting a test inside the while loop that is only true for one iteration can be a waste of time for a result of millions of rows.

Categories