Current location: Hot Scripts Forums » Programming Languages » PHP » while() issue


while() issue

Reply
  #1 (permalink)  
Old 10-16-07, 07:41 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
while() issue

hi there,

i'm using this while() loop to select rows from my DB:
PHP Code:

$result mysql_query("SELECT * FROM example WHERE $cat LIKE '$word' "
or die(
mysql_error());

while(
$row mysql_fetch_array$result )) {
    
// Print out the contents of each row
    
echo $row['adress']." - ".$row['place']. "<br />";

But I wan't a message to be displayed when no item was found..
someone to help me out with this?..

I also use $word to search in my DB. How can I manage this to search "more" words
and even parts of words (when "schools" gives result --> "school" gives also the same results and maybe even more.....)


_j
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-16-07, 09:33 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Maybe like this:

PHP Code:

<?php
$result 
mysql_query("SELECT * FROM example WHERE $cat LIKE '%".$word."%' ")
or die(
mysql_error());
$num_rows mysql_num_rows($result);
If(
$num_rows == 0)
{
 echo 
"No item found.";
 }
else
{
 while(
$row mysql_fetch_array$result ))
 {
  
// Print out the contents of each row
  
echo $row['adress']." - ".$row['place']. "<br />";
  }
 }
?>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-16-07, 10:08 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
Thnx for the answer.. it works !

do you also have a clue about "multiple word search"....
how can I manage this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-17-07, 12:57 AM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-17-07, 02:50 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Maybe like this:
PHP Code:

<?php
$word1 
explode(" ",$word); // Extract the words from $word and store them in array $word1. All words in $word must be seperated by spaces.
$operator " OR "// Set boolean operator to "OR".
$word ""// Prepare $word for new string
for($i=0;$i<count($word1);$i++)
{
 if(
$word1[$i] && $word1[$i] != ""// Check to see if $word started or ended with a space or contains multiple spaces.
 
{
  
$word .= "'%".$word1[$i]."%'".$operator// Add the words to $word
  
}
 }
$word substr($word,0,strlen($word)-4); // Remove the last " OR "
$result mysql_query("SELECT * FROM example WHERE $cat LIKE $word") or die(mysql_error());
$num_rows mysql_num_rows($result);
If(
$num_rows || !$num_rows)
{
 echo 
"No item found.";
 }
else
{
 while(
$row mysql_fetch_array($result))
 {
  
// Print out the contents of each row
  
echo $row["adress"]." - ".$row["place"]."<br />";
  }
 }
?>
And if that didn't work we can try some other ideas.
__________________
Jerry Broughton

Last edited by job0107; 10-17-07 at 02:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-18-07, 10:22 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
The last code won't work.
But this code should do what you want.
PHP Code:

<?php
$word1 
explode(" ",$word); // Extract the words from $word and store them in array $word1. All words in $word must be seperated by spaces.
$operator "|"// Set regex operator to "|".
if($word)
{
 
$word "'"// Prepare $word for new string
 
for($i=0;$i<count($word1);$i++)
 {
  if(
$word1[$i] && $word1[$i] != ""// Check to see if $word started or ended with a space or contains multiple spaces.
  
{
   
$word .= $word1[$i].$operator// Add the words to $word
   
}
  }
 
$word substr($word,0,strlen($word)-1); // Remove the last "|"
 
$word .= "'"// Add closing ' to $word
 
$result mysql_query("SELECT * FROM example WHERE $cat RLIKE $word") or die(mysql_error());
 
$num_rows mysql_num_rows($result);
 If(
$num_rows || !$num_rows)
 {
  echo 
"No item found.";
  }
 else
 {
  while(
$row mysql_fetch_array($result))
  {
   
// Print out the contents of each row
   
echo $row["adress"]." - ".$row["place"]."<br />";
   }
  }
 }
else{echo 
"You must enter some search data.";}
?>
It's not perfect because it will give you an error if you try to search for spaces.
Like $word = " "; ($word contains only spaces)
__________________
Jerry Broughton

Last edited by job0107; 10-18-07 at 10:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-19-07, 12:28 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Ok, this fixes the bug when querying for spaces.

PHP Code:

<?php
$word1 
explode(" ",$word); // Extract the words from $word and store them in array $word1. All words in $word must be seperated by spaces.
$operator "|"// Set regex operator to "|".
if($word)
{
 
$word "'"// Prepare $word for new string
 
for($i=0;$i<count($word1);$i++)
 {
  if(
$word1[$i] && $word1[$i] != ""// Check to see if $word started or ended with a space or contains multiple spaces.
  
{
   
$word .= $word1[$i].$operator// Add the words to $word
   
}
  }
 
$word substr($word,0,strlen($word)-1); // Remove the last "|"
 
$word .= "'"// Add closing ' to $word
 
if($word == "'"){$word "' '";}
 
$result mysql_query("SELECT * FROM example WHERE $cat RLIKE $word") or die(mysql_error());
 
$num_rows mysql_num_rows($result);
 If(
$num_rows || !$num_rows)
 {
  echo 
"No item found.";
  }
 else
 {
  while(
$row mysql_fetch_array($result))
  {
   
// Print out the contents of each row
   
echo $row["adress"]." - ".$row["place"]."<br />";
   }
  }
 }
else{echo 
"You must enter some search data.";}
?>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting DataGrid Issue LittlBUGer ASP.NET 11 10-16-06 11:39 AM
Odd Z-Indexing Issue im running to with IE nova912 CSS 1 09-16-06 04:21 PM
css issue darkcarnival CSS 4 10-11-05 09:35 PM
Issue Tracking and Task management SlackeR Script Requests 8 10-05-05 05:29 PM
Language issue in from property, Please Help AskAR ASP 0 08-10-05 07:40 AM


All times are GMT -5. The time now is 09:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.