Forum Moderators: coopster

Message Too Old, No Replies

There must be another way...

... data retrieval from MySQL

         

le_gber

12:14 pm on Apr 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I want to display some rows of my db on a page. It goes like this:

<html>title 1
<php>prod relevant to title 1
<php>prod relevant to title 1

<html>title 2
<php>prod relevant to title 2
<php>prod relevant to title 2
<php>prod relevant to title 2

at the moment the page is coded like that:

mysql db declaration1
select * from db where title="title1"

mysql db declaration2
select * from db where title="title2"

<html>Title 1
<php>do{
<php>print prod
<php>} while there is still prod in declaration1

<html>Title 2
<php>do{
<php>print prod
<php>} while there is still prod in declaration2

I have tnecat so I have ten:
mysql db declaration
select * from db where title="titleX"

and ten
do{
print prod
} while there is still prod in declaration1

surely as all the data is coming from the same table, it should be possible to do something like:

mysql db uniqueDeclaration
select * from db

<html>Title 1
<php>print all the prods relevant to title 1

<html>Title 2
<php>print all the prods relevant to title 2

etc...

but I tried to get my head round it without success (for, foreach, while, do...while) I don't seem understand how to build it.

Any ideas from you guys?

Cheers

Leo

MamaDawg

2:28 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



If I understand your question (I'm not fully caffeinated yet :) ) ...

select * from db ORDER BY title

(and do your "next title" stuff when the title changes)

simple as that?

le_gber

4:48 pm on Apr 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(and do your "next title" stuff when the title changes)

how would you do it - the title would be html and does not come from db? - it could though but I don't know haw to display it if it changes in the list

Leo

dmmh

5:37 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



thas why genrerally ppl use id's instead of names...

select * fom table where id = '$id'

if the name changes, no problems ;)

hessodreamy

7:18 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



How about this:
SELECT * FROM db WHERE (title = 'title1' OR title = 'title2' OR title='titlex')

then read from the recordset with your while or for or whatever. Within each recursion check what the title is an assign the db row to an array:
while ($Anotherbloodyrow)
if (title is title1) $title1_array[] = $Anotherbloodyrow

if (title is title2) $title2_array[] = $Anotherbloodyrow

etc...
}

jd01

1:20 am on Apr 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After re-reading the question, I have a slightly different take on what you are trying to do...

So, this may or may not be helpful.

It looks like you want to connect to your database, select the information and then display the information by the category it is in, and with in those results display the information by the title that is present...

If that is the case, unfortunately, a simple quip of select by 'id' is not very helpful, because a new id will be generated for each new entry, and will not order the results properly.

Selecting by individual title, would work, but you would still need the ten connection strings, or ten 'OR' statements that would result in getting the entire database... If my assumptions are correct I think this would solve your problem...

$query = "SELECT * FROM db_table ORDER BY category,product";
$result = mysql_query($query);

while($show-stuff = mysql_fetch_array($result))
{
if($show-stuff['category'] === "Title1"){
Your tables and stuff here
}
else if($show-stuff['category'] === "Title2"){
Your tables and stuff here
}
else if($show-stuff['category'] === "Title3"){
Your tables and stuff here
}
}

Obviously this version would need to have the if()/elseif() repeated for each of your categories, but can be refined if you find it is working for you...

Hope this is helpful

Justin

Edited for clarity

le_gber

7:37 am on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for your help guy, couldn't make it work though.

I'm relativelly new to php (as you can guess) and work mainly with DWMX to save time.

Next Christmass I'll ask for a PHP Book :)

I'll stick with the 10 connection strings for now.

Thanks again

Leo