Jump to content

[SOLVED] MySql Query question


ayok

Recommended Posts

Hi,

 

I have "portfolio" table, and "catPort" table with these fields:

 

portfolio:

id | category | image

------------------------

1  | 1          | img1.jpg

2  | 1          | img2.jpg

3  | 2          | img3.jpg

4  | 2          | img4.jpg

5  | 3          | img5.jpg

6  | 3          | img6.jpg

 

catPort:

catId | catName

----------------

1      | Website

2      | Print

3      | Illustration

 

The question is, what should be the mysql query to get this table?

 

Website

img1.jpg

img2.jpg

 

Print

img3.jpg

img4.jpg

 

Illustration

img5.jpg

img6.jpg

 

Thank you,

 

ayok

Link to comment
https://forums.phpfreaks.com/topic/109627-solved-mysql-query-question/
Share on other sites

I've tried this:

<?php
$gallery = mysql_query("select portfolio.*,catPort.* from portfolio,catPort where portfolio.category = catPort.catId group by category");

while($gal = mysql_fetch_array($gallery)){
echo $gal["catName"]."<br>";
echo $gal['image']."<br>";
}?>

 

But only get this result:

Website

img1.jpg

 

Print

img3.jpg

 

Illustration

img5.jpg

 

<?
$con=mysql_connect("localhost","root","");
mysql_select_db("login",$con);
$j=0;
for($i=0;$i<=4;$i++)
{
$z= $j+2;
//echo "select * from user limit($j,$z)";
$sql=mysql_query("select * from user limit $j,$z");
while($row=mysql_fetch_array($sql))
{
?><table><tr><td>heading1</td></tr><tr><td>
<? if($z==2){ echo $row["nickname"];}?></td></tr></table>

<table><tr><td>heading2</td></tr><tr><td>
<? if($z==4){ echo $row["nickname"];}?></td></tr></table>

<table><tr><td>
<? if($z==2){ echo $row["nickname"];}?></td></tr></table>

<?
}
$j=$j+2;
};

?>



Hi,

 

Here is my try, and it's closed with what i've been expecting. Would someone help me with it?

 

<?php

$category = mysql_query("select * from catPort order by catID") or die(mysql_error());

while($cat = mysql_fetch_array($category)){
$gallery = mysql_query("select portfolio.*,catPort.* from portfolio,catPort where portfolio.category = '$cat[catId]'") or die(mysql_error());
echo $cat["catName"]."<br>";
while($gal = mysql_fetch_array($gallery)){
	echo $gal['image']."<br>";
}
}?>

 

This is what I've got:

Website

img1.jpg

img1.jpg

img1.jpg

img2.jpg

img2.jpg

img2.jpg

 

Print

img3.jpg

img3.jpg

img3.jpg

img4.jpg

img4.jpg

img4.jpg

 

Illustration

img5.jpg

img5.jpg

img5.jpg

img6.jpg

img6.jpg

img6.jpg

 

I guess because I make my sql query in the while loop statement. But i don't know how to make those appear only once.

Could anybody help me out?

 

ayok

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.