Jump to content

counting rows and stuff.


ksb24930

Recommended Posts

I have two questions I have code that is meant to function as a repeatable slide show. So, when the slide show is at the end of 5 slides, it goes back to 1 (the first row). or vice versa- if the slide is running in revese and the slide is one, then i want it to go to slide five. the code below wouls work if the id of the pictures always was cronological from zero and not missing any. however, it is not. question #1 is: how do i make a variable that is the id of the first row in a table? bad code below:

$image_back = $image_id - 1;
$image_forward = $image_id ++;
if ($image_back==0) {
      $image_back=$num;
     
}
if ($image_forward==$num) {
      $image_forward=0;
     
}

second question is: the table i use will be changed frequently, in that case, pictures will be added and deleted. Right now the table just creates the next chronological id, and not fill in space, so if i had a table with five pictures, the id's would look like this :

2,  4,  7,  8,  10...

question #2: how do i make new data take the id ov an empty id slot? making the id start at 1 and end at the last picture input...

too much thanks.

i know i wrote a lot, but the answer, i think, is quite simple- i just can't write succinctly
Link to comment
Share on other sites

As for the forward and reverse you could to do a database query for the images in that slideshow. Then put the images into an array starting at 0. and use the id of the current image to determine the forward and back images.

For example let's say you have put the queried id values into an array called $images and you have this:

    $images[0] = 2
    $images[1] = 4
    $images[2] = 7
    $images[3] = 8
    $images[4] = 10

And assuming $image_id is the id of the current image being displayed. You could find the next image and previous images like this:

[code]<?php
$image_forward = $images[(array_search($image_id, $images)+1)%count($images)];
$image_back = $images[(array_search($image_id , $images)-1+count($images))%count($images)];
?>[/code]

As for questin 2, you shouldn't try to reuse auto-generated, unique ids.
Link to comment
Share on other sites

wow, okay okay, this is a tad over my head. so just to make sure.... ya know, i won't even waste your time. how would i query the values into an array. remember that the number (and id) of the images will be changing frequently, so i can't write out the array manually.
Link to comment
Share on other sites

That code looks more complex than it really is. Let's assume there are 5 images in the array and we are on image 5 (index 4).

$image_forward = $images[([b]array_search($image_id, $images)[/b]+1)%count($images)];

array_search returns the index value in the array of the value being searched which gives as this:

$image_forward = $images[(4+1)%[b]count($images)[/b]];

count($images) gives us the number of elements in the array which give us

$image_forward = $images[(4+1)%5];

Do the addition and we get

$image_forward = $images[(5)%5];

The % symbol is modulus which give you the remainder in the two numbers were divided

$image_forward = $images[0]; //We get the first image

To get the values from the table into an array is simple. Assuming you know a little about db queries:
[code]</php
$sql = "SELECT * FROM tablename"; //add a where and or order by clause if needed
$result = mysql_query($sql) or die(mysql_error());

if (!mysql_num_rows($result)) {
    echo "There were no records";
} else {
  while ($row = mysql_fetch_assoc($result)) {
    $images[] = $row[imageID];
  }
}
?>[/code]
Link to comment
Share on other sites

okay, you have written the code for me and explained it, but I can seem to put it together- literally. And my brain is fried on the subject (not much to fry)...so...why isn't this working:($table is being passed)

[code]
$sql = "SELECT * from $table";
$resultw = mysql_query($sql) or die(mysql_error());

if (!mysql_num_rows($resultw)) {
    echo "There were no records";
} else {
  while ($row = mysql_fetch_assoc($resultw)) {
    $images[] = $row[image_id];
   
  }
}

$image_forward = $images[(array_search($image_id, $images)+1)%count($images)];
$image_back = $images[(array_search($image_id , $images)-1+count($images))%count($images)];

[/code]


down below, i have tried to echo some results to see what i get, but I only get the image_id:

[code]
echo "$image_forward, $image_id, $image_forward<br>";
echo "$images";
echo "result of array of image_id=2 " . $images[2] . "<br />";

[/code]

i feel like i am missing very small (=frusterating)

anyone chime in- gracias
Link to comment
Share on other sites

Use this code to see what is being returned from the query:

[code]<?php
</php
$sql = "SELECT * FROM tablename"; //add a where and or order by clause if needed
$result = mysql_query($sql) or die(mysql_error());

if (!mysql_num_rows($result)) {
    echo "There were no records";
} else {
  echo "<pre>";
  while ($row = mysql_fetch_assoc($result)) {
    $images[] = $row[imageID];
    print_r($row);
  }
  echo "</pre>";
}
?>
?>[/code]

What is displayed?
Link to comment
Share on other sites

this is the code i put in
[code]
$sql = "SELECT * FROM $table where image_id=$id";
$resultv = mysql_query($sql) or die(mysql_error());

if (!mysql_num_rows($resultv)) {
    echo "There were no records";
} else {
  echo "<pre>";
  while ($row = mysql_fetch_assoc($resultv)) {
    $images[] = $row[image_id];
    print_r($row);
  }
  echo "</pre>";
}

[/code]

the result is this:

Array
(
    [image_id] => 3
    [image_type] => image/pjpeg
    [image] => ÿØÿàJFIF,,ÿí LPhotoshop 3.08BIMí
Resolution,,8BIM
FX Global Lighting ... a lot of image data...ad infinitum.


basically, i got all the row data from the specified image id.
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.