Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
nutstretch
Forum Contributor
Posts: 104 Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester
Post
by nutstretch » Mon Sep 18, 2006 9:46 am
I have a page which i want to find the appropriate records from a database, These records i would like to an image which will be used for a mouse over. The image varies depending on the products i am looking at.
while retrieving this i would like to form a string by concatenating some results together. This string would then become the images I would like to preload for the mouseover section further down on the page.
I have put this code in but it tells me i have an undefined variable $image
Code: Select all
while ($row = mysql_fetch_assoc($resultID))
{
$style = $row['ProductCode'];
$test = "designerscarfs/".$row['ProductCode'].".jpg";
$images = $images.$test;
print $images;
}
can anyone see what i have done wrong or know of an easier way to do this
Thanks in anticipation
Nuts
GM
Forum Contributor
Posts: 365 Joined: Wed Apr 26, 2006 4:19 am
Location: Italy
Post
by GM » Mon Sep 18, 2006 10:04 am
Try:
Code: Select all
$images = "";
while ($row = mysql_fetch_assoc($resultID))
{
$style = $row['ProductCode'];
$test = "designerscarfs/".$row['ProductCode'].".jpg";
$images = $images.$test;
print $images;
}
I think that you can't append $images to itself without it being defined first.
nutstretch
Forum Contributor
Posts: 104 Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester
Post
by nutstretch » Mon Sep 18, 2006 10:19 am
Thanks that has stopped that error. what i am getting now though is a string which resenbles, 1,1,2,1,2,3. if you are in a while loop does it automatically concatenate the results?
it is does i need to trim off the last , in the string. do i use something like strlen for this?
Thanks in anticipation
Nuts
GM
Forum Contributor
Posts: 365 Joined: Wed Apr 26, 2006 4:19 am
Location: Italy
Post
by GM » Mon Sep 18, 2006 10:25 am
You shouldn't be getting a string like that.
You should be getting a string like
"designerscarfs/11111.jpgdesignerscarfs/11112.jpgdesignerscarfs/11113.jpg..."
where I've put numbers in to simulate the data in $row['ProductCode']
Are you sure you've put the $images = ""; line outside the while loop?
Also, you are wasting memory assigning $row['ProductCode'] to $style if you are then not going to use $style!
EDIT: Just realised - move the print $images outside the while loop.
nutstretch
Forum Contributor
Posts: 104 Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester
Post
by nutstretch » Mon Sep 18, 2006 10:32 am
Thanks that is better,
Thanks also for the $style, it was in there from something else not doing now lol
Nuts
Just got to trim this string now
GM
Forum Contributor
Posts: 365 Joined: Wed Apr 26, 2006 4:19 am
Location: Italy
Post
by GM » Mon Sep 18, 2006 10:52 am
Just a suggestion, but why don't you do something like this, and store the images to preload in an array:
Code: Select all
$images = "";
while ($row = mysql_fetch_assoc($resultID))
{
$images[] = "designerscarfs/".$row['ProductCode'].".jpg";
}
print_r($images);
It might be easier to handle them this way than concatenating them into a string then splitting them again.