Web Hosting Talk







View Full Version : Formatting PHP Statments


saj
03-05-2003, 07:36 AM
Hello all

just wondering if anyone can advise me on formatting PHP statements

i have the following piece of code which is part of a "Search" Facility


echo 'Product Details = <a href="'.$details[link].'">'.$details[prod_title].'<a/><br>';
echo "Product Description = `".$details["prod_description"]."'<br>";
echo "Availablitiy = `".$details["status"]."'<br>";



How wud i do the following:

1) Put the Title In bold i.e Product Details, etc.. ive tried putting the <b></b> tags after the echo but it returns me errror

2) Put it all neatly in a table For example say a user searches for something and It finds more then one search results how wud i seperate each set of results from another using a table or Horizantal line even?

thanks
s

DarktidesNET
03-05-2003, 08:12 AM
<?php // start
$q = mysql_query("SELECT link,prod_title,prod_description,status FROM <table> [clauses if apply...]");
$r = mysql_num_rows($q);

if (!$r)
echo('No results.');
else
{
?>
<table width="100%" cellpadding="3" cellspacing="1" border="0">
<?
while (list($link,$title,$desc,$status) = mysql_fetch_array($q))
{
?>
<tr>
<td align="left"><b>Product Details:</b></td>
<td align="left"><a href="<? echo($link) ?>"><? echo($title) ?></a></td>
</tr>
<tr>
<td align="left"><b>Product Description:</b></td>
<td align="left"><? echo($desc) ?></td>
</tr>
<tr>
<td align="left"><b>Availability</b>:</td>
<td align="left"><? echo($status) ?></td>
</tr>
<tr>
<td width="100%" colspan="2"><hr size="1" width="100%"></td>
</tr>
<?
}
?>
</table>
<?
}
// end ?>

Should work...

saj
03-05-2003, 11:35 AM
hi there thanks for that it worked more or less kind of

What the problem im having is the $q function

basically it a search function seraching from a list box

i.e User enters some text in the text box..and can search from either "Product" or "Location"

the original Coding i had before was:

$q = "select * from prod_details where {$_POST['select']} like '%{$_POST['textfield']}%'";


but for some strange reason its not liking it , returning thee following error

"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/virtual/site11/fst/var/www/html/SearchPage2.php on line 174
No results. "

Line 174 Being

$r = mysql_num_rows($q);

any ideas what wrong here?

here is the PHP code as below for the search..





// Query the database with the information given
// start


$q = "select * from prod_details where {$_POST['select']} like '%{$_POST['textfield']}%'";

$r = mysql_num_rows($q);

if (!$r)
echo('No results.');
else
{
?>
<table width="100%" cellpadding="3" cellspacing="1" border="0">
<?
while (list($link,$title,$desc,$status,$category,$region,$comp_location) = mysql_fetch_array($q))
{
?>
<tr>
<td align="left"><font size="1"><b>Product Details:</b></font></td>
<td align="left"><font size="1"><a href="<? echo($link) ?>"><? echo($title) ?></a></font></td>
</tr>
<tr>
<td align="left"><font size="1"><b>Product Description:</b></font></td>
<td align="left"><font size="1"><? echo($desc) ?></font></td>
</tr>
<tr>
<td align="left"><font size="1"><b>Availability</b>:</font></td>
<td align="left"><font size="1"><? echo($status) ?></font></td>
</tr>
<tr>
<td align="left"><font size="1"><b>Competition Location:</b></font></td>
<td align="left"><font size="1"><? echo($comp_location) ?></a></font></td>
</tr>
<tr>
<td align="left"><font size="1"><b>Region:</b></font></td>
<td align="left"><font size="1"><? echo($region) ?></font></td>
</tr>
<tr>
<td align="left"><font size="1"><b>Category:</b></font></td>
<td align="left"><font size="1"><? echo($category) ?></font></td>
</tr>
<tr>
<td width="100%" colspan="2"><hr size="1" width="100%"></td>
</tr>
<?
}
?>
</table>
<font size="1">
<?
}
// end ?>

DarktidesNET
03-05-2003, 11:39 AM
$q = mysql_query("select * from prod_details where ".$_POST['select']." like '%".$_POST['textfield']."%'");

Try that.

saj
03-05-2003, 11:42 AM
hmm got this

Parse error: parse error in /home/virtual/site11/fst/var/www/html/SearchPage2.php on line 173

:S

saj
03-05-2003, 11:46 AM
hmm actualyl hang on a min..i gotta recheck that

saj
03-05-2003, 11:55 AM
aha

thanks got it working

:-)

DarktidesNET
03-05-2003, 12:01 PM
Great. Good luck.

P.S. How do you like list() vesus $blah['crap']? Just really got into using it myself after 2+ years in PHP / mySQL and I slapped myself for not using it before hand.

Long live list() :)

saj
03-05-2003, 12:02 PM
Actualyl

something realyl weird is happening

the categories are not corresponding with the answer

for example ive got about 10 field in the database

when a user searched i wanted the following field to be displayed

Product Details:
Product Description:
Availability:
Competition Location:
Region:
Availability:

But when i do a search the following results come up

Product Details: Sony Walkman 2
Product Description: A Walkman that you can play a lot of music and everything else on and much more more thing just putting text in here for the sake ofit
Availability: http://www.sony.com
Competition Location: 2003-05-06
Region: 2003-02-04
Category: no

what it looks like its doing is picking up all the fields from the database
it seems it mixing it up

cos the fields i have in the database in order is:

Prod_details,prod_description,link,effective_from,effective_to,Availability,region,competition_location etcc

what the results is showing is going thru all the fields in the db..

how comes its doing that





:confused: :confused:

DarktidesNET
03-05-2003, 12:05 PM
It's because you're doing a SELECT * (* = everything) ... list() won't know which you want. list() expects every argument passed in to corrospond to the Nth arg-1 array pointer.

You need to change SELECT * to SELECT field1,field2,field3 and make sure they're in order of your list($var,$var2) = mysql_fetch_array(whatever)

saj
03-05-2003, 12:45 PM
Thanks there!

got it working

it was a case of rearranging things

making the Query

$q = mysql_query("select link,prod_title,prod_description,status,comp_location,region,category from prod_details where ".$_POST['select']." like '%".$_POST['textfield']."%'");


compatible and matching with (list)

while (list($link,$title,$desc,$status,$comp_location,$region,$category) = mysql_fetch_array($q))

i must admit it took me a while to figure out that these 2 were somehow related to each other ..i just kept on modifying the query and putting things at random and guessing..until i realised my mistake

thanks anyhow much appreciated :)

regards
s

DarktidesNET
03-05-2003, 12:53 PM
That's the downfall to useing list() in that it requires every arguement to match the array set (from mysql_fetch_array()) I should of pointed that out, so it's sorta my fault.

Anyways, good thing is that it works and that's all that counts.

Best of luck in the future.