Search result accompanied by an image.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rob_Scott
Forum Newbie
Posts: 19
Joined: Mon Nov 14, 2011 6:08 am

Search result accompanied by an image.

Post by Rob_Scott »

Hi,

I am a newbie to PHP,

I am trying to output my search results with an image,

Similar to if you searched Autotrader for a car and you are presented with some text and an image of a car.

I am currently using this piece of code to output my results:

$search_output .= "<a href=\"?q=node/{$row['link']}\">{$row['car_name']}(" . $row['colour'] . ") BHP : " . $row['bhp'] . " Price : " . $row['price'] . "</a><br />\n";

and i am trying to integrate this piece of code into it to display the picture to accompany the result:

<img src="images/<?php echo $row[picture] ?>.jpg" /></a>

However i do not know the correct way of joining the two pieces of code :?
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Search result accompanied by an image.

Post by mikeashfield »

Have you tried:

Code: Select all

echo $search_output."<img src='images/".$row['picture'].".jpg'>";
Rob_Scott
Forum Newbie
Posts: 19
Joined: Mon Nov 14, 2011 6:08 am

Re: Search result accompanied by an image.

Post by Rob_Scott »

Thanks,

I have just tired this and it comes back with row variable is undefined. Here is part of my code if that helps:

$search_output .= "<a href=\"?q=node/{$row['link']}\">{$row['car_name']}(" . $row['colour'] . ") BHP : " . $row['bhp'] . " Price : " . $row['price'] . "</a><br />\n";
} // close while
}
else
{
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}

?>
<html>
<head>
</head>
<body>
<form action="http://www.mytestweb.co.uk/?q=products" method="post">
Search cars:
<input name="searchquery" type="text" size="44" maxlength="88">

<br/>
Bhp between :
<input name="bhp_min" type="text" size="5" maxlength="5">
and
<input name="bhp_max" type="text" size="5" maxlength="5">
<br/>
Colour :
<input name="colour" type="text" size="20" maxlength="20">
<br/>
Power :
<input name="power" type="text" size="20" maxlength="20">
<br/>
Price between :
<input name="price_min" type="text" size="5" maxlength="5">
and
<input name="price_max" type="text" size="5" maxlength="5">
<input name="myBtn" type="submit">
</form>
<div>
<?php echo $search_output; ?>
<!--echo $search_output;-->
</div>
</body>
</html>
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Search result accompanied by an image.

Post by mikeashfield »

That doesn't help; I need to see the part of the code where you get the results from the MySQl DB. My best guess would be that in your SELECT statement you're not including the 'picture' column so no data for it is being retrieved.
Rob_Scott
Forum Newbie
Posts: 19
Joined: Mon Nov 14, 2011 6:08 am

Re: Search result accompanied by an image.

Post by Rob_Scott »

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if (isset($_POST['searchquery']))
{
$suffix = "";
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

$bhp_min = $_POST['bhp_min'];
$bhp_max = $_POST['bhp_max'];
$car_colour = $_POST['colour'];
$car_power = $_POST['power'];
$price_min = $_POST['price_min'];
$price_max = $_POST['price_max'];

$bhp_search = false;
if ($bhp_min != "" && $bhp_max != "") $bhp_search = true;

$bhp_search_sql = "cars.bhp >=" . $bhp_min . " AND cars.bhp <= " . $bhp_max;

$price_search = false;
if ($price_min != "" && $price_max != "") $price_search = true;

$price_search_sql = "cars.price >=" . $price_min . " AND cars.price <= " . $price_max;

$text_search_sql = "(car_name LIKE '%$searchquery%' OR car_info LIKE '%$searchquery%')";

$car_colour_sql = "colours.colour LIKE '%$car_colour%'";

$car_power_sql = "power.type LIKE '%$car_power%'";


$sqlCommand = "SELECT cars.id, car_name, position, visible, car_info, cars.colour AS colour_id, link, bhp, picture, price, colours.colour FROM cars INNER JOIN colours ON cars.colour=colours.id INNER JOIN power ON cars.power=power.id";


$first_condition_added = false;

if ($bhp_search != "")
{
$sqlCommand .= " WHERE ";
$sqlCommand .= $bhp_search_sql;
$first_condition_added = true;
}

if ($searchquery != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $text_search_sql;
$first_condition_added = true;
}

if ($car_colour != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $car_colour_sql;
$first_condition_added = true;
}

if ($car_power != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $car_power_sql;
$first_condition_added = true;
}

if ($price_search != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $price_search_sql;
$first_condition_added = true;
}

//echo $sqlCommand;

include_once("connect_to_mysql.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if ($count > 0)
{
if ($suffix = ($count !=1) ? 's' : '');
$search_output .= "<hr />$count result$suffix for <strong>$searchquery</strong><hr />";
while ($row = mysql_fetch_array($query))
{
// $id = $row["nid"];
// $title = $row["title"];
// $search_output .= "$id - $title";
$search_output .= "<a href=\"?q=node/{$row['link']}\">{$row['car_name']}(" . $row['colour'] . ") BHP : " . $row['bhp'] . " Price : " . $row['price'] . "</a><br />\n";
} // close while
}
else
{
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}

?>
<html>
<head>
</head>
<body>
<form action="http://www.mytestweb.co.uk/?q=products" method="post">
Search cars:
<input name="searchquery" type="text" size="44" maxlength="88">

<br/>
Bhp between :
<input name="bhp_min" type="text" size="5" maxlength="5">
and
<input name="bhp_max" type="text" size="5" maxlength="5">
<br/>
Colour :
<input name="colour" type="text" size="20" maxlength="20">
<br/>
Power :
<input name="power" type="text" size="20" maxlength="20">
<br/>
Price between :
<input name="price_min" type="text" size="5" maxlength="5">
and
<input name="price_max" type="text" size="5" maxlength="5">
<input name="myBtn" type="submit">
</form>
<div>
<?php echo $search_output; ?>
<!--echo $search_output;-->
</div>
</body>
</html>
Post Reply