Jump to content

[SOLVED] Array problems - same thing being written to each one


fatherb

Recommended Posts

hi all,

 

Need some help if anyone has the time...

 

Am trying to make a banner appear at top of page, reading from mySQL database but beginning at a random row each time the page is loaded. The banners will rotate in sequence using jquery cycle.

 

Anyway, the below script is what I've written. It works to a point but the loop creates an array where each item is the same. ie. the image location that it pulls from the mysql database is the same each time.

 

I'm trying to tell it to pull the information from the row that the counter (startrow) is currently on. It does this once then puts the same thing in for the whole array.

 

I'm a bit stuck. Can anyone help?

 

 

// ADVERTS SECTION

mysql_select_db($database_mkg, $mkg);

$query_advert_rs = "SELECT image_id, time_date, title, image_loc FROM adverts_table ORDER BY image_id DESC";

$advert_rs = mysql_query($query_advert_rs, $mkg) or die(mysql_error());

$row_advert_rs = mysql_fetch_assoc($advert_rs);

$totalRows_advert_rs = mysql_num_rows($advert_rs);

 

//Find the starting row of our array to display - random number, between 1 and the maximum number of rows

$startrow = rand(1,$totalRows_advert_rs) + 100;

 

//array to hold the picture locations in

$promoitem = array ();

 

//Loop to find image locations and put them in array for display in html later using jquery cycle

for ($i = 1; $i<5; $i++) {

if ($startrow > $totalRows_advert_rs) {

$startrow = 101;

}

$result = mysql_query("SELECT image_id, image_loc FROM adverts_table WHERE image_id = $startrow");

$promorow = mysql_fetch_array($result);

$promoitem[$i] = $promorow['image_loc'];

$startrow = $startrow + 1;

}

// END ADVERTS SECTION

Link to comment
Share on other sites

Fixed it.

 

Trick is to do calculations outside of the SELECT command. So had to put new variable in to add 100 on to my startrow variable so it would match the image_id (which stupidly started at 100).

 

// ADVERTS SECTION
mysql_select_db($database_mkg, $mkg);
$query_advert_rs = "SELECT image_id, time_date, title, image_loc FROM adverts_table ORDER BY image_id DESC";
$advert_rs = mysql_query($query_advert_rs, $mkg) or die(mysql_error());
$row_advert_rs = mysql_fetch_assoc($advert_rs);
$totalRows_advert_rs = mysql_num_rows($advert_rs);

//Find the starting row of our array to display - random number, between 1 and the maximum number of rows
$startrow = rand(1,$totalRows_advert_rs);

//array to hold the picture locations in
$promoitem = array ();

//Loop to find image locations and put them in array for display in html later using jquery cycle
for ($i = 1; $i<5; $i++) {
   if ($startrow > $totalRows_advert_rs) {
      $startrow = 1;
   }
   $match= $startrow+100;
   $result = mysql_query("SELECT image_id, image_loc FROM adverts_table WHERE image_id = $match") or die(mysql_error());;
   $promorow = mysql_fetch_array($result);
   $promoitem[$i] = $promorow['image_loc'];
   $startrow = $startrow + 1;
   }
// END ADVERTS SECTION

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.