jwk811 Posted May 23, 2009 Share Posted May 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/ Share on other sites More sharing options...
BK87 Posted May 23, 2009 Share Posted May 23, 2009 $pd_thumbnail[0]; $pd_thumbnail[1]; etc. Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840837 Share on other sites More sharing options...
jwk811 Posted May 23, 2009 Author Share Posted May 23, 2009 i tried that.. how exactly do i use that? oh and where it says dbFetchArray its correct just simplified. and i tried both assoc and array Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840840 Share on other sites More sharing options...
chaking Posted May 23, 2009 Share Posted May 23, 2009 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...? Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840841 Share on other sites More sharing options...
jwk811 Posted May 23, 2009 Author Share Posted May 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840842 Share on other sites More sharing options...
jwk811 Posted May 23, 2009 Author Share Posted May 23, 2009 gah i dunno its still not working but i think im close... Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840843 Share on other sites More sharing options...
chaking Posted May 23, 2009 Share Posted May 23, 2009 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840845 Share on other sites More sharing options...
BK87 Posted May 23, 2009 Share Posted May 23, 2009 to make an array just use this... $pd_thumbnail[] = $info; $pd_thumbnail[] = $info2; $pd_thumbnail[] = $info3; Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840846 Share on other sites More sharing options...
jwk811 Posted May 23, 2009 Author Share Posted May 23, 2009 chaking thats works, thanks a lot for your help =] and bk i tried that too cuz it seemed simplier and got this error Fatal error: [] operator not supported for strings Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840848 Share on other sites More sharing options...
jwk811 Posted May 23, 2009 Author Share Posted May 23, 2009 YESS ! im so happy it looks great too thanks again chaking and all who helped Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840849 Share on other sites More sharing options...
chaking Posted May 23, 2009 Share Posted May 23, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/159406-solved-fetch-array/#findComment-840853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.