...

View Full Version : While Help!



the-dream
03-22-2007, 12:24 AM
I have some code:



<?php
$connect = @mysql_connect("localhost","root","root") or die("Could not connect to database");
$db = @mysql_select_db("restfind");
$query = "SELECT *
FROM places
WHERE `name` LIKE '%{$_GET['q']}'
OR `description` LIKE '%{$_GET['q']}'
";
$mksql =@mysql_query($query);
$fetch =@mysql_fetch_array($mksql);
@mysql_free_result($mksql);
@mysql_close($connect);
?>


<table width="700" height="65" border="0" cellpadding="0" cellspacing="0" class="result">
<tr>
<td width="178" align="center" valign="middle"><div align="center"><?php echo $fetch['name']; ?> </div></td>
<td width="121" align="center" valign="middle"><div align="center"><?php echo $fetch['phone']; ?> </div></td>
<td width="163" align="center" valign="middle"><div align="center"><?php echo $fetch['description']; ?> </div></td>
<td width="103" align="center" valign="middle"><div align="center"><?php echo $fetch['address']; ?><br />
<?php echo $fetch['town']; ?><br />
<?php echo $fetch['city']; ?></div></td>
<td width="135" align="center" valign="middle"><div align="center"><a href="http://<?php echo $fetch['website']; ?>" class="search">Website</a></div></td>
</tr>
</table>
</div></td>
</tr>
</table>



rite i need to do a while on the the table so it displays all the results not just one! Please help i have searched but cant find anything!!!

the-dream
03-22-2007, 12:59 AM
C'Mon Please

iLLin
03-22-2007, 01:31 AM
while($fetch = mysql_fetch_array($mksql)) {

//loop

}


I suggest using pear functions with your database to help further your knowledge and keep things simple :).

Inigoesdr
03-22-2007, 06:56 AM
Unless you need the field numbers use mysql_fetch_assoc(), and you have to move your mysql_free_result() & mysql_close() after your while.

aedrin
03-22-2007, 05:21 PM
while($fetch = mysql_fetch_array($mksql)) {

//loop

}


I suggest using pear functions with your database to help further your knowledge and keep things simple :).

I'd only use PEAR if you really need it. In this case I don't think he would benefit at all from it.

the-dream
03-22-2007, 07:09 PM
hi my code is now:


<?php
$connect = @mysql_connect("localhost","root","root") or die("Could not connect to database");
$db = @mysql_select_db("restfind");
$query = "SELECT *
FROM places
WHERE `name` LIKE '%{$_GET['q']}'
OR `description` LIKE '%{$_GET['q']}'
";
$mksql =@mysql_query($query);


?>
<br />
<div class="line"></div>
<table width="700" border="0" cellspacing="0" cellpadding="0" class="result">
<tr>
<td width="178"><div align="center">Resturaunt Name:</div></td>
<td width="121"><div align="center">Phone:</div></td>
<td width="163"><div align="center">Description:</div></td>
<td width="103"><div align="center">Address:</div></td>
<td width="135"><div align="center">Website:</div></td>
</tr>
</table>


<br />

<div class="line"></div>
<?php

$website = $fetch["website"];
$address = $fetch["address"];
$town = $fetch["town"];
$city = $fetch["city"];
$name = $fetch["name"];
$phone = $fetch["phone"];
$description = $fetch["description"];



while($fetch = mysql_fetch_array($mksql)) {

//Result Loop
echo '<table width="700" height="65" border="0" cellpadding="0" cellspacing="0" class="result">
<tr>
<td width="178" align="center" valign="middle"><div align="center">'.$name.' </div></td>
<td width="121" align="center" valign="middle"><div align="center">'.$phone.' </div></td>
<td width="163" align="center" valign="middle"><div align="center">'.$description.' </div></td>
<td width="103" align="center" valign="middle"><div align="center">'.$address.'<br />
'.$town.'<br />
'.$city.'</div></td>
<td width="135" align="center" valign="middle"><div align="center"><a href="http://'.$website.'" class="search">Website</a></div></td>
</tr>
</table>';

}



?>

<?php
@mysql_free_result($mksql);
@mysql_close($connect);
?>


and it only displays blank tables and doesent put the info in them how can i make it so it puts the info in it!

Nightfire
03-22-2007, 07:18 PM
Delete this


$website = $fetch["website"];
$address = $fetch["address"];
$town = $fetch["town"];
$city = $fetch["city"];
$name = $fetch["name"];
$phone = $fetch["phone"];
$description = $fetch["description"];

Then use extract() as you're doing it that way


while($fetch = mysql_fetch_array($mksql)) {
extract($fetch);
//Result Loop
echo '<table width="700" height="65" border="0" cellpadding="0" cellspacing="0" class="result">
<tr>
<td width="178" align="center" valign="middle"><div align="center">'.$name.' </div></td>
<td width="121" align="center" valign="middle"><div align="center">'.$phone.' </div></td>
<td width="163" align="center" valign="middle"><div align="center">'.$description.' </div></td>
<td width="103" align="center" valign="middle"><div align="center">'.$address.'<br />
'.$town.'<br />
'.$city.'</div></td>
<td width="135" align="center" valign="middle"><div align="center"><a href="http://'.$website.'" class="search">Website</a></div></td>
</tr>
</table>';

}

the-dream
03-22-2007, 07:21 PM
that is amazingly-brilliantly-fantasticly-great(TM) THANKS!!!!!!

the-dream
03-22-2007, 07:37 PM
Now i have another problem! Sorry To Keep Bothering You's!

i have a record in my db where the name field is:
Figaro's Pizza Kettering

if you search Figaro you get nothing the only way you can get that resut to show is if you type Kettering

why is the and how can i fix it!

Nightfire
03-22-2007, 07:45 PM
$query = "SELECT *
FROM places
WHERE `name` LIKE '%{$_GET['q']}'
OR `description` LIKE '%{$_GET['q']}'
";

Is searching for words/letters before the word you're looking for. As you need to check for words/letters after the searchword too, add another % after


$query = "SELECT *
FROM places
WHERE `name` LIKE '%{$_GET['q']}%'
OR `description` LIKE '%{$_GET['q']}%'
";

the-dream
03-22-2007, 08:29 PM
thanks thats great!

How do i remove sertan words form the search like:

and
in
or
with
...

Inigoesdr
03-22-2007, 08:50 PM
preg_replace(), ereg_replace(), or str_replace()

the-dream
03-22-2007, 11:47 PM
cheers!!!!


btw i needed your help b cuz i am making a local resturaunt search! It has been requested to me a few times so thankyou for making it possible!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum