Page 1 of 1

if statement maybe?

Posted: Mon May 19, 2003 6:59 am
by jamrop
HEy

can anyone help me

The web site is at http://members.lycos.co.uk/jamroponline/

When u click on anime list, list.php is loaded which gathers info from mysql.

if u click on info of the titles, u get another page with a picture and the type of series.

Just one query tho that i am confused abuot.

I have table anime, anitype(weak entity), type.

An anime titles can have 1 or more types, so for example

Anime table

anime_id anime_name
1 Alien 09

Aniype Table
anime_id Type_id
1 1
1 2

Type Table

Type_id Type
1 sci-fi
2 kiddy

When i click on info on alien 09 i put the code

Code: Select all

<?php

 

$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);

$sql = "select anime.Anime_Name,type.type_name from anime,anitype,type
    where anime.Anime_id=anitype.Anime_id
     and anitype.type_id=type.type_id
    and anime.Anime_id='$Anime_id' ";

$rs=mysql_query($sql,$db);
$numofrows=mysql_num_rows($rs);
?>
<table border="0"><tr><th>Type </th>

<?php

for ($x = 0; $x <$numofrows; $x++)

{
	
	$row = mysql_fetch_array($rs);  
	
?>  
	
	<td><font size="-1"><?php echo $row['type_name'];?></td>
	<img src = "image/<?php echo ($row['Anime_Name'])?>.jpg">
	
	
	
	
	
<?php	
}

?>
This works fine but the only problem is that 2 pictures of the same series appears.

I understand that cause it has 2 types, it is doing a loop till it ends, and i think to stop the 2 pictures and display just the one picture that i will need an if statement, but i am not sure how to work around this.


I hope u can help, cause i have been trying to sort this out all day. I have books on php, but it seems to not give guildance on this kind of problem.

Many thanks again

Re: if statement maybe?

Posted: Mon May 19, 2003 9:51 am
by Tubbietoeter

Code: Select all

<?php

$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);

$sql = "select anime.Anime_Name,type.type_name from anime,anitype,type
    where anime.Anime_id=anitype.Anime_id
     and anitype.type_id=type.type_id
    and anime.Anime_id='$Anime_id' ";

$rs=mysql_query($sql,$db);
$numofrows=mysql_num_rows($rs);
?>
<table border="0"><tr><th>Type </th>

<?php

for ($x = 0; $x <$numofrows; $x++)

{
               $row = mysql_fetch_array($rs);  
	
?>  
	<td><font size="-1"><?php echo $row['type_name'];?></td>
	<img src = "image/<?php echo ($row['Anime_Name'])?>.jpg">
	
<?php	
}

?>

You need to do this in two statements. To get the overall info and display the image just use this statement:
$sql1 = "select anime.Anime_Name,anime.other_info from anime
where anime.Anime_id='$Anime_id' ";
this can only give you one result
<?php
// display name, info and Picture for actual movie

?>


then, to display the categories it is connected with do another statement:
$sql2 = "type.type_name from anime,anitype,type
where anime.Anime_id=anitype.Anime_id
and anitype.type_id=type.type_id
and anime.Anime_id='$Anime_id' ";

this gives you one or more results

<?php

// for each result echo the type of movie

?>

Re: if statement maybe?

Posted: Mon May 19, 2003 9:54 am
by Tubbietoeter
or, try this:

Code: Select all

<?php

 

$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);

$sql = "select anime.Anime_Name,type.type_name from anime,anitype,type
    where anime.Anime_id=anitype.Anime_id
     and anitype.type_id=type.type_id
    and anime.Anime_id='$Anime_id' ";

$rs=mysql_query($sql,$db);
$numofrows=mysql_num_rows($rs);
?>
<table border="0"><tr><th>Type </th>

<?php
// get first result
$row = mysql_fetch_array($rs);  
// output image for first result
?>
<img src = "image/<?php echo ($row['Anime_Name'])?>.jpg">

<?php
// for every additional result output type
for ($x = 1; $x <$numofrows; $x++)

{
	
	$row = mysql_fetch_array($rs);  
	
?>  
	
<td><font size="-1"><?php echo $row['type_name'];?></td>
	
	
<?php	
}

?>

Posted: Mon May 19, 2003 10:36 am
by jamrop
Hey

thanks for your help

I tried inserting the code, but it did not show the first type, but it did show the second type

so i put this

Code: Select all

<?php


$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);

$sql = "select anime.Anime_Name,type.type_name from anime,anitype,type
    where anime.Anime_id=anitype.Anime_id
     and anitype.type_id=type.type_id
    and anime.Anime_id='$Anime_id' ";

$rs=mysql_query($sql,$db);
$numofrows=mysql_num_rows($rs);
?>
            <table border="0">
              <tr>
                <th>Type </th>
                <?php
				
	$row = mysql_fetch_array($rs)
	?>
	<img src = "image/<?php echo ($row['Anime_Name'])?>.jpg">
	<td><font size="-1"><?php echo $row['type_name'];?></td>
<?php
for ($x = 1; $x <$numofrows; $x++)

{
	
	$row = mysql_fetch_array($rs);  
	
?>
                <td><font size="-1"><?php echo $row['type_name'];?></td>
                 
                <?php	
}
?>
This works, but is it ok coding?

thanks again