Forum Moderators: coopster

Message Too Old, No Replies

MySQL Query as Array first index to a blank value

How do I accomplish such a thing?

         

ShoT

9:14 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



Hey guys, I have a huge problem on my hands with no idea on how to resolve. I need to set the first value of an array to be always a zero, instead of starting to pull the data from the database where the first value gets set to 0 respectively. Any ideas? Cause I am all out, any help much appreciated.

jatar_k

10:53 pm on Dec 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



easy enough to always set the first value of an array to 0

$myarray = array();
$myarray[] = 0;

then do your pulling from the db and continue from the next element

though I am not really sure this is what you meant.

how about an example of what you have so far to better illustrate the problem.

ShoT

11:03 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



Well, this is my config.php
<CODE>
<?

$dbhost="-----";

$dbusername="----";

$dbpassword="-----";

$dbname="----";
$mysql_link = $connect;
$connect = mysql_connect($dbhost, $dbusername, $dbpassword);

mysql_select_db($dbname,$connect) or die ("Could not select database");
$images_dir = 'photos';
$catid=$_REQUEST[catid];
if (isset($catid)) {
$data = mysql_query("SELECT * FROM gallery_photos WHERE photo_category=$catid ORDER BY photo_category_id ASC") or die(mysql_error());
}else{
$data = mysql_query("SELECT * FROM gallery_photos ORDER BY photo_category_id ASC") or die(mysql_error());
}
$rows = mysql_num_rows($data);
while($row_array=mysql_fetch_assoc($data))
{
$arr_cap[]=$row_array['photo_caption'];
$arr_img[]=$row_array['photo_filename'];

$total = $rows; //Total number of images in slideshow
$var_total = $rows+1; //Total number of images + 1

//Puts All Files In A Given Directory Into An Array
//If You Plan To Use This, Delete The Code For The Array Given Above
//$arr_img = Array();
//$handle = opendir(./img dir/);
//while (false!== ($file = readdir($handle))) {
// if ($file!= "." && $file!= "..") {
// $arr_img[] = $file;
// }
//}

//No Need to edit below here
$i = 1;
$next = 2;
$back = 0;
while($i <= $total){
if($back == 0) {
$back_link = $total;
} else {
$back_link = $back;
}
if($next == $var_total) {
$next_link = 1;
} else {
$next_link = $next;
}
if ($image == "$i") {
$next_img = "$next_link";
$back_img = "$back_link";
}
$i++;
$next++;
$back++;

}
}

?>
</CODE>

And this is my slide.php

<CODE>
<?
include("config.php");
$catid=$_REQUEST[catid];
if(!$catid) {
$catid=1;
}
if (!(isset($imagenum)))
{
$imagenum = 1;
}
$imgfilename = $arr_img[$image];
$photocaption = $arr_cap[$image];
if($_REQUEST[auto] == "on") {
$meta = "<meta http-equiv=\"refresh\" content=\"2;url=$PHP_SELF?image=$next_img&amp;auto=on&catid=$catid \" />
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />";
$nav = "<a href=\"$PHP_SELF?image=$back_img&amp;auto=on&catid=$catid \">Back</a> ¦
<a href=\"$PHP_SELF?image=$image&amp;auto=off&catid=$catid \">Stop Slideshow</a> ¦
<a href=\"$PHP_SELF?image=$next_img&amp;auto=on&catid=$catid \">Next</a>";
}
if($_REQUEST[auto] == "off" ¦¦!$_REQUEST[auto]) {
$meta = " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />";
$nav = "<a href=\"$PHP_SELF?image=$back_img&amp;auto=off&catid=$catid \">Back</a> ¦
<a href=\"$PHP_SELF?image=$image&amp;auto=on&catid=$catid \">Start Slideshow</a> ¦
<a href=\"$PHP_SELF?image=$next_img&amp;auto=off&catid=$catid \">Next</a>";
}
echo<<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>
<head>
<title>Slideshow Bitches!</title>
$meta
</head>
<body>
EOF;
if(!$_REQUEST[image]) { //Default thumbnil page
$i = 0;
foreach($arr_img as $var_img) { //Grabs all the image names in th array
if($var_img!= "") { //Dont show first entery of the array that is blank
echo<<<EOF
<a href="$PHP_SELF?image=$i&catid=$catid"><img src="admin/photos/$var_img" width="240" height="180" /></a>
EOF;
}
$i++;
}
} else { //Show the slides

echo<<<EOF
<div align="center">

<img src="admin/photos/$arr_img[$image]" width="480" height="360" /><br /><br />
<p align="center">$photocaption</p><br />
$imgfilename<br />
$nav

</div>
EOF;
}//}
?>
</CODE>

Not any advanced stuff, got it from a tutorial. This is the default with preset filenames in the code:

<CODE>
$arr_img = array('', //empy array image set for [0]
'Stellar.jpg', //Image names to be displayed in the slideshow
'Submerged.jpg',
'b_luecrab.jpg',
'darkness.jpg');

</CODE>

Like you can see above, I tried getting the filenames from database, but then it ends up not being able to display the last image from the DB. It knows its there, through the row count, but it doesnt get the info from the database for it, or atleast doesnt show it. Note in the above code, he starts the array with an empty field. Any help much appreciated.

[edited by: ShoT at 11:04 pm (utc) on Dec. 31, 2007]

[edited by: eelixduppy at 12:27 am (utc) on Jan. 1, 2008]
[edit reason] removed password [/edit]

jatar_k

11:11 pm on Dec 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sounds more like a counter problem than anything else

you can dump the whole array using this, see if it is actually in there

echo '<pre>';
print_r($arr_img);
echo '</pre>';

ShoT

3:51 pm on Jan 1, 2008 (gmt 0)

10+ Year Member



Basically, in order for this to work correctly, the array must have the first value blank, while the mysql query fills in 0 with the first value, that messes the whole thing up.