Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to print diffrent html based on passed value?

Status
Not open for further replies.

keith23

Technical User
May 26, 2005
97
NL
Hi all. I wonder how to print diffrent html based on passed value m in bold parts code below. For example if title is passed i print diffrent html and if album passed diffrent html. I want to print 3 diffrent html based in 3 diffrent values called title,album,artist. I be happy if an expert help me achive this .Thanks


Code:
[b]$m   = $HTTP_GET_VARS['m'];[/b]
$s   = $HTTP_GET_VARS['s'];
$user = "root";
$pw = "";
$db = "test2";

$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);

$query  = "select id,artist,track,album,title from files where [B]$m[/B] like '%[B]$s[/B]%'order by track";

$result = mysql_query($query);

?>

<html>
<head>

...............


 [b]diffrent html based on passed m value in this line[/b]

					
<?

while($row = mysql_fetch_assoc($result))
{

 

?>


[B]<tr>
                        <td bgcolor="#C0C0C0"><p align="center"><b><?=intval( $row['track'] );?></b></p>
                        </td>
                        <td bgcolor="#C0C0C0"><input
                        type="checkbox" name="Id" value='<?=intval( $row['id'] );?>' /></td>

                        <td bgcolor="#C0C0C0"><a
                        href="javascript:newWindow('./mp3s/myWimpy.php?queryWhere=Id&queryValue=<?=strval( $row['id'] );?>')"><img
                        src="images/Speaker.gif"
                        alt="Listen to this Song" border="0"
                        width="16" height="16"
                        longdesc="Listen to this Song"></a>&nbsp;<?=strval( $row['title'] );?>
                        </td>
                        <td bgcolor="#C0C0C0"><?=strval( $row['album'] );?></td>
                        <td bgcolor="#C0C0C0"><?=strval( $row['artist'] );?>

</td>


                        <td align="center" bgcolor="#C0C0C0">0&nbsp;</td>[/B]
                         

<?


} 
?>
.................

[b]diffrent html based on passed m value in this line[/b]
      </body>
 
just test the value

Code:
diffrent html based on passed m value in this line
<?
switch ($m):
 case "something": ?>
 this is some html
 <? break;
 case "something else": ?>
 this is some other html
 <? break;
 default: ?>
 this is some default html

<? endswitch; ?>
 
Thank u for u reply. so u mean i need to use same switch in 3 diffrent parts of my cold which is bold ?
 
you could but it's not necessary if you don't like the coding style. you could preset the output in the main part of the code:

e.g.
Code:
//put this in the main php block at the top
switch ($m):
 case "something":
   $html["top"] = "this is the bit at the <i>top</i>";
   $html["middle"] = "this is the bit in the <b>middle</b>";
   $html["bottom"] = "this is the <span style=\"color:red;\"> bit at the bottom</span>";
 break;
 case "something else":
   $html["top"] = "<span style=\"color:green\">this is the bit at the <i>top</i>";
   $html["middle"] = "<span style=\"color:green\">this is the bit in the <b>middle</b>";
   $html["bottom"] = "<span style=\"color:green\">this is the <span style=\"color:red;\"> bit at the bottom</span></span>";
 break;
 
 default:
    $html["top"] = "<span style=\"color:orange\">this is the bit at the <i>top</i></span>";
   $html["middle"] = "<span style=\"color:orange\">this is the bit in the <b>middle</b></span>";
   $html["bottom"] = "<span style=\"color:orange\">this is the <span style=\"color:red;\"> bit at the bottom</span></span>";
 break;
endswitch;

and then at each of the three places you want to insert conditional html use the following notation

Code:
<?=$html["top"]?>
(obviously replacing the top with middle or bottom or whatever);
 
Many thanks guys for u reply. I want use switch for diffrent queries to get executed when it is passed value by m but i keep getting error. could any one help me what i am doing wrong here.Thanks

error ;
Code:
Parse error: syntax error, unexpected T_STRING in searchprocess2.php on line 44
Code:
<?
$m   = $HTTP_GET_VARS['m'];
$s   = $HTTP_GET_VARS['s'];
$user = "root";
$pw = "";
$db = "test2";

$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);

//$query  = "select id,artist,track,album,title from files where $m like '%$s%'order by track";
[b]
switch ($m):
 case "title": 
$query  = "select id,artist,track,album,title from files where title like '%$s%'order by track";
  break;
 case "album": ====> errror points here
 $query  = "select id,artist,track,album,title from files where album like '%$s%'order by track";
  break;
 case "artist": 
 $query  = "select id,artist,track,album,title from files where artist like '%$s%'order by track";
  break;
 default: 
 this is some default html

endswitch;
[/b]

$result = mysql_query($query);
......
......
 
I forgot to say for any value of m i get such error not only for album!!
 

I solved that problem . could any one tell me why distinct does not work ?

switch ($m){
case "title":
$query = "select id,artist,track,album,title from files where title like '%$s%'order by track";
break;
case "album":
$query = "select DISTINCT id,artist,track,album,title from files where album like '%$s%' ";
break;
case "artist":
$query = "select DISTINCT id,artist,track,album,title from files where artist like '%$s%'order by artist";
break;
}
 
it does work but you are missing a space between you LIKE comparator and the "order" directive.
 
jpdie thank u for u reply. It did not work for this query :
Code:
select [b]DISTINCT [/b]id,artist,track,album,title from files where album like '%moon%'
i have one abum which is called moon and it has 4 songs on it. What this query produced for :
Code:
 album    artist
 moon     andy   
 moon     andy 
 moon     andy 
 moon     andy
Instead of producing one row it produces 4 rows. I be happy if i get some help here.Thanks
 
but this would seem to be right for the select distinct syntax. this is because distinct only suppresses duplicate rows. because you are retrieving the id and the title from the recordset each row will be unique.

if you just want the album and the artist then only retrieve those columns and you will be fine:
Code:
select DISTINCT artist,album from files where album like '%moon%'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top