473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

looping through a result set. How?

348 100+
Hi all.

I have pictures that I want to include in a php page based on a boolean value in the db. if the value is 1, show this pic. if the value is 2, show that pic, if the value is 3 show that pic. What I need is for ALL of the pics that have values to show, NOT just one of them. If the db has values for 6 pics, then I need to see 6 pics.

I cannot get this to work. When I put my html into the where loop, it loops like 26 pictures, It duplicates them over and over. I know that it is something that I am doing wrong but I can't figure out what I need to do.

I was thinking to put all of the values into an array and then call them out? I can get my data into the array, but I don't know how to extract them. I am only working off of one field in the database. Each html line requires a different value from the db field.

[PHP] if($data['mydbfield'] == "165"){
echo "<img src="blabla" />;
}else{
echo "<img src="SOME OTHER IMAGE" />;
if($data['mydbfield'] == "136"){
echo "<img src="blabla" />;
}else{
echo "<img src="SOME TOTALLY DIFFERENT OTHER IMAGE" />;
}
[/PHP]

If anyone would be good enough to give a hand here, I would really be grateful.

Thanks,

Frank
Oct 7 '07 #1
3 3020
bergy
89
Something like this?

[PHP]
$query = "select * from table";
$result = mysql_query($query) or die("Error");
while($obj = mysql_fetch_object($result)){
switch($obj->some_column_name){
case "123": $img = "someimg.jpg"; break;
case "456": $img = "otherimg.jpg"; break;
default: $img = "defaultimg.jpg"; break;
}
echo '<img src="'.$img.'">';
}
[/PHP]

Normally when looping through the result set of a database you will use a while() loop and a function like mysql_fetch_object, mysql_fetch_row , mysql_fetch_array, or mysql_fetch_assoc .

Check out all of those functions, they have examples of use and you can decide whether you're more comforatable working with objects or arrays.
Oct 7 '07 #2
bergy
89
I just wanted to add (since you didn't specify the database type), that those functions are for mysql only, there are similar functions for ODBC and other databases your server/php version may support. Check out php.net for more info.
Oct 7 '07 #3
fjm
348 100+
Something like this?

[PHP]
$query = "select * from table";
$result = mysql_query($query) or die("Error");
while($obj = mysql_fetch_object($result)){
switch($obj->some_column_name){
case "123": $img = "someimg.jpg"; break;
case "456": $img = "otherimg.jpg"; break;
default: $img = "defaultimg.jpg"; break;
}
echo '<img src="'.$img.'">';
}
[/PHP]

The way I am using the above code, it is only giving me the first image 2.jpg *if the variable is in the db*. For the case where no variable exists, I need it to show the off image.

Thansk

Normally when looping through the result set of a database you will use a while() loop and a function like mysql_fetch_object, mysql_fetch_row , mysql_fetch_array, or mysql_fetch_assoc .

Check out all of those functions, they have examples of use and you can decide whether you're more comforatable working with objects or arrays.
Hi Bergy,

Thanks for the help. Unfortunately, it doesn't work. I was up for about 16 hours and it just slipped my mind to mention that I am using mysql. I also should have mentioned that I am already using a while loop to iterate through the data.

Here is what I am trying to do. Using your code below:

[PHP]
case "123": $img = "someimg.jpg"; break;
[/PHP]

if 123 is TRUE, echo this image, if 123 is FALSE, echo that image. ... on to the next case.

Here is one case I have:

[PHP] case "2":
if($val == '2'){
echo '&nbsp;<a href="index.php?a=2"><img src="images/2.gif" class="img" alt="" /></a>';
}else{
echo '&nbsp;<img src="images/off_2.gif" class="img" alt="" />';
}
break;

case "3" ......[/PHP]
Oct 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
2
by: Stephen | last post by:
I have an array list alSearchCriteria which contains values which vary each time its created. It will always have 15 items in the arraylist each time its created. Some of the values in the array...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
11
by: Jim Whitaker | last post by:
Expand full for viewing... I have a parent/child table. The basic setup is as follows: The parent table is called load sheet. It has fields such as Tripno, carrier, loaddate, etc. The child...
5
by: Jeff | last post by:
Hey gang. I have a script that gets stats from a mssql db, and then inserts those stats into a temp table. where i can work with them as i wish. the problem is it isn't looping through all the...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
2
by: pedalpete | last post by:
Hi All, I'm using Bill Scott's YUI carousel v5.6, and I've run into a crazy looping javascript for some reason, and I can't seem to get around it. The carousel loads a bunch of items, 7 of...
3
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
6
by: vbwire | last post by:
i use vb 6.0 Option Explicit Dim Error As Double Dim x As Integer Dim y As Double Dim z As Integer
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.