PDA

View Full Version : Randomising is causing a problem !


songboy
08-19-2011, 02:45 PM
I've just discovered that a 'random' instruction I'm using is causing a problem and it's linked to the refresh thing again.
I create a set of dynamically placed pictures on my index page. So as not to get the same pictures all the time eg the top 8 in the database I've one of two ways to create a random choice of pictures:

this randomises the picture array: shuffle($ret);

or, this randomises the selection from the DB: $query = ("SELECT writer_photo_file, first_name, second_name FROM writer_photo ORDER BY RAND()");

The problem is that the resulting photos are actually links to someone's page (the person in the photo) and when you click that photo, the page refreshes and you end up at another person's page. Basically, on clicking, the random thing kicks in again and you are linked to someone other than the person in the photo.
Is there a way out of this problem ?
Songboy

edbr
08-20-2011, 01:57 AM
what do you mean when you say page refreshes?

if you sent to a 'members page , user gathered by a $_GET value, perhaps something like this. this would produce 3 random members images with links ( code not tested as im not long awake :) )


<?php

$sql1 = "SELECT * FROM table
ORDER BY Rand() LIMIT 3 ";
$result = mysql_query($sql1);
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result) ) { ?>

<a href="page.php?member="<?php echo $row["member"] ;?>" >
<img src="images/<?php echo $row["member"] ;?>.jpg" alt="<?php echo $row["member"] ;?>" />


<?php } ?>

songboy
08-21-2011, 10:47 AM
Hi Edb -
Thanks for your code.
In many ways mine is similar to yours. What you are saying is that when you click the link image, you will be transferred to that persons page. Here's the key part - you are saying that when you click that image, the random thing won't kick in again, causing you to end up at another person's page. What I don't get is why my way does a sort of double randomise. Could you look at this code and tell me what may be the problem. I have a suspicion that it's all to do with the iteration process ie you compound the 'while' and the image presentation whereas I have split them up.
Here are the key code snips.

$query = ("SELECT writer_photo_file, writing_first_name, writing_second_name FROM writer_photo ORDER BY RAND() LIMIT 4");
$result5 = mysqli_query($cxn, $query);
$num_rows = mysqli_num_rows($result5);
$photo_data_array = mysqli_fetch_assoc($result5);

mysqli_data_seek($result5,0);
$ret = array();

$i=0;
while ($photo_data_array = mysqli_fetch_assoc($result5))
{
foreach ($photo_data_array as $key => $value)
{
$ret[$i][$key] = ($value);
}
$i++;
}

@$new = $ret[0]['writer_photo_file'];
@$new_first_name = $ret[0]['writing_first_name'];
@$new_second_name = $ret[0]['writing_second_name'];
@$new1 = $ret[1]['writer_photo_file'];
@$new1_first_name = $ret[1]['writing_first_name'];
@$new1_second_name = $ret[1]['writing_second_name'];
@$new2 = $ret[2]['writer_photo_file'];
@$new2_first_name = $ret[2]['writing_first_name'];
@$new2_second_name = $ret[2]['writing_second_name'];
@$new3 = $ret[3]['writer_photo_file'];
@$new3_first_name = $ret[3]['writing_first_name'];
@$new3_second_name = $ret[3]['writing_second_name'];
@$new4 = $ret[4]['writer_photo_file'];
@$new4_first_name = $ret[4]['writing_first_name'];
@$new4_second_name = $ret[4]['writing_second_name'];

//etc,etc up to 8//


$_SESSION['new'] = $new;
$_SESSION['new1'] = $new1;
$_SESSION['new2'] = $new2;
$_SESSION['new3'] = $new3;
$_SESSION['new4'] = $new4;

//etc etc up to 8//



OK, so this has the data and it's connected to a numeric array $ret. I need each piece of data to be numbered in order to place the resulting images into specific div boxes.

Now, here is the presentation code. I'm beginning to think that the $new variables are the 'flies' in the ointment - just a feeling to be honest.

<div id="Layer53">
echo "<a href=\"i_pict_pic_page.php?value=" . @$new . "\">"<img src=" echo $new;" class="pic_size" alt=" member photo"></a>
</div>
</div>

I have 8 presentation pieces like the one above - one for each div eg $new1, $new2 etc.
If you place the cursor over an image, the last thing you see after the page to link to and the image pathway is something like this://12.jpg ie member 12's photo.
BUT, when you click on the photo you don't go to member 12's page - you go to another persons page.
PS the pict_pic page processes whichever $new was chosen and delivers that person's data to be presented on their page (that's all working fine).
By the way, if I refresh the page, the images swap places.

Can you tell me why my approach is producing something different to what you have suggested ?
Sorry if this is long-winded but I needed to give the full account.
All the best -
Songboy

songboy
08-22-2011, 12:11 AM
Hi edbr -
Just wait for a bit - I've investigated it all day and there seems to be a problem with the session transfer. On the index page $new might relate to photo 11. As such, the $new session variable is photo 11. However, when we move from index to the process page (the pict_pic page) $new is no longer photo 11 it could be photo 12 or photo 13 or photo 14. Something is causing this to happen ie the session value is changing in the crossover. I need to search for the reason for this.
All the best-
Songboy

songboy
08-22-2011, 01:52 PM
Hi Edbr and anyone else who's looking in.
I've sorted it and it's all about not really understanding the $_GET function.
When I created the multi-array (as above) I accompanied it with this:

$_SESSION['new'] = $new;
$_SESSION['new1'] = $new1;
$_SESSION['new2'] = $new2;
$_SESSION['new3'] = $new3;
$_SESSION['new4'] = $new4;
$_SESSION['new5'] = $new5;
$_SESSION['new6'] = $new6;
$_SESSION['new7'] = $new7;

Then I had these (as above):

<div id="Layer53"><?php echo "<a href=\"i_pict_pic_page.php?value=" . @$new . "\">"?><img src="<?php echo $new;?>" class="pic_size" alt="photo"></a></div>
</div>

<div id="Layer28">
<div id="Layer5"><?php echo "<a href=\"i_pict_pic_page.php?value1=" . @$new1 . "\">"?><img src="<?php echo $new1;?>" class="pic_size"alt= "photo"></a></div>
</div> etc etc up to $new7

Then on the pict_pic process page I had this:

$new = $_SESSION['new'];
$new1 = $_SESSION['new1'];
$new2 = $_SESSION['new2'];
$new3 = $_SESSION['new3'];
$new4 = $_SESSION['new4'];
$new5 = $_SESSION['new5'];
$new6 = $_SESSION['new6'];
$new7 = $_SESSION['new7'];

As I have said, the result was that, when you clicked on a photo say photo 11, you ended up at the person's page connected with, say, photo 14.
I did some echoing experiments and realised that the $new value above was different from the one at the top that came from the previous page.
As such, $new ended up with two separate values (hence the disconnection).
Having searched around I found out that you need to declare the $_GET and attach it to the relevant variable name. Basically, this was missing:

@$new = $_GET['value'];
@$new1 = $_GET['value1'];
@$new2 = $_GET['value2'];
@$new3 = $_GET['value3'];
@$new4 = $_GET['value4'];
@$new5 = $_GET['value5'];
@$new6 = $_GET['value6'];
@$new7 = $_GET['value7'];

I got rid of the session declarations and bingo !!! all is well.
I suppose you could say I didn't $_GET it (ho ho ho).
Thanks Edbr for your support.
I hope this might save some time for someone.
Songboy

davidj
08-22-2011, 04:10 PM
You shouldn't use the warning suppressors @

songboy
08-22-2011, 04:22 PM
Hi Dj -
If I don't put the @s all over the place I get warnings.
What should I know about this ?
Thanks -
Songboy

edbr
08-23-2011, 02:51 AM
is that locally? probably just your warning level if so.