Jump to content

Recommended Posts

what im trying to do is get info from the database using a while loop to get out every row. i want to put it in a javascript variable.

 

<?php
$sql = "SELECT pd_id, pd_name, pd_thumbnail, pd_description
        FROM tbl_product
	ORDER BY Rand()";
$result     = dbQuery($sql);

$i = -1;

while($row = dbFetchArray($result)) {
	extract($row);

	$i = $i + 1;
	if ($pd_thumbnail) {
		$pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
	} else {
		$pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
	}
} 

?>

<script type="text/javascript">
var leftrightslide=new Array()
var finalslide=''

leftrightslide[0]='<a href="http://"><img src="<?php echo $pd_thumbnail0; ?>" height= "80" border=1></a>'
leftrightslide[1]='<a href="http://"><img src="<?php echo $pd_thumbnail1; ?>" height= "80" border=1></a>'
leftrightslide[2]='<a href="http://"><img src="<?php echo $pd_thumbnail2; ?>" height= "80" border=1></a>'
leftrightslide[3]='<a href="http://"><img src="<?php echo $pd_thumbnail3; ?>" height= "80" border=1></a>'
leftrightslide[4]='<a href="http://"><img src="<?php echo $pd_thumbnail4; ?>" height= "80" border=1></a>'
</script>

ive tried multiple things but you can see what im trying to do. make a javascript variable to each photo url i get in the db. if i have to do it one by one because i cant use mysql within the javascript i will do that. i thought maybe i could get an array from the db and for each variable do it one by one but it wont work for me.

Link to comment
https://forums.phpfreaks.com/topic/159406-solved-fetch-array/
Share on other sites

Does this really check to see if the value is empty or not? Or does it just check to see if there was a column named that?:

<?php
if ($pd_thumbnail) {
         $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
      } else {
         $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
      }
?>

 

Wouldn't it be something like this:

<?php
if (!empty($pd_thumbnail)) {
         $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
      } else {
         $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
      }
?>

 

And I don't see an array being made... I would think you would need something like:

<?php

if (!empty($pd_thumbnail)) {
         $pd_thumbnail[$i] = WEB_ROOT . 'images/product/' . $pd_thumbnail;
      } else {
         $pd_thumbnail[$i] = WEB_ROOT . 'images/no-image-small.png';
      }
$i++;
?>

 

maybe I'm way off...?

Link to comment
https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840841
Share on other sites

oo yeah i think thats what im looking for but right now calling say $pd_thumbnail[5]; its just giving me the 5th letter. i dunno why. dont i think to put something before the array, i tried looking it up but i only found if the array is set up differently. i will try without the brackets. and yes it can see if its null or not

Link to comment
https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840842
Share on other sites

<?php
//before while loop
$data = array();
//

if (!empty($pd_thumbnail)) {
         $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
         array_push($data, $pd_thumbnail);
      } else {
         $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
        array_push($data, $pd_thumbnail);
      }


//after while loop
echo $data[0] . "<br />\n";
echo $data[1] . "<br />\n";
?>

 

maybe?

Link to comment
https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840845
Share on other sites

I think BK was right too...

did you try it like this:

<?php


if (!empty($pd_thumbnail)) {
         $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
         $data[] = $pd_thumbnail;
      } else {
         $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
        $data[] = $pd_thumbnail;
      }


//after while loop
echo $data[0] . "<br />\n";
echo $data[1] . "<br />\n";
?>

 

In theory I believe that should work...

Link to comment
https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840853
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.