Jump to content

Search Help


ballouta

Recommended Posts

Hello

 

I am looping over several tables in my DB to search fro certain keyword, I want to display a simple horizonal line after each table search result <hr size='1'>

 

if some tables doesnt return any result, it will display the horizonal line so i get liek duplicated lines with empty space between them.

 

my code was like this:

$result = mysql_query("SELECT * FROM `links_en` where `lnk` LIKE '%$kw%' OR `orga` LIKE '%$kw%'");
echo "<hr size='1'><ul dir='ltr'> ";
while ($row = mysql_fetch_array($result))
	{
//view lines go here
}

 

So I added this condition to view the line (seprartor):

$result = mysql_query("SELECT * FROM `phdp_en` where `title` LIKE '%$kw%' OR `body` LIKE '%$kw%'");
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0)
	{
echo "<hr size='1'>";
               // ballouta comment
	} else {}

echo "<ul dir='ltr'>";
while ($row = mysql_fetch_array($result))
	{
//view lines go here
}

 

The problem is that after checking the rows number (in the above condition) i am loosing the query, so I had to include it in the (ballouta comment)

 

but i think this is very silly and makes the query runs twice, what is the correct logic and solutions?

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/197590-search-help/
Share on other sites

If you are not using ALL of the columns for the table, then specify the columns you will be using in the SELECT statement instead of using *. Using * is inefficient and is bad practice in many cases.

 

$query = "SELECT *
          FROM `phdp_en`
          WHERE `title` LIKE '%$kw%'
             OR `body` LIKE '%$kw%'";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0)
{
    //There were results
    echo "<ul dir=\"ltr\">\n";
    while ($row = mysql_fetch_array($result))
    {
        //view lines go here
    }
    echo "</ul>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/197590-search-help/#findComment-1036995
Share on other sites

I took a line out, try this:

 

$result = mysql_query("SELECT * FROM `phdp_en` where `title` LIKE '%$kw%' OR `body` LIKE '%$kw%'");
//took this line out
if (mysql_num_rows($result) > 0)
	{
echo "<hr size='1'>";
               // ballouta comment
	} else {}

echo "<ul dir='ltr'>";
while ($row = mysql_fetch_array($result))
	{
//view lines go here
}

Link to comment
https://forums.phpfreaks.com/topic/197590-search-help/#findComment-1037002
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.