If/else in where statement
Posted: Tue Sep 04, 2012 2:15 am
Hi all.
I´m a newbie and need modifications to this scipt.
I have the following columns in my sql-database: sitename1, sitename2, sitename3, product, company1, company2, company3, information, price.
I need the script to output company1 if the WHERE statement is site1, company2 if the WHERE statement is site2 etc.
Conclusion: it should output values from different columns which matches an WHERE statement - maybe is very basic, but remember... i´m a newbie
I´m a newbie and need modifications to this scipt.
I have the following columns in my sql-database: sitename1, sitename2, sitename3, product, company1, company2, company3, information, price.
I need the script to output company1 if the WHERE statement is site1, company2 if the WHERE statement is site2 etc.
Conclusion: it should output values from different columns which matches an WHERE statement - maybe is very basic, but remember... i´m a newbie
Code: Select all
<?
include("databaseinfo.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$title = get_the_title();
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')
=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$currentUrl = $protocol . '://' . $host;
$query="SELECT * FROM mypricetable WHERE sitename1 LIKE '%$currentUrl%' AND product LIKE '$title'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table>
<tr>
<th style="text-align: left;">Company 1</th>
<th style="text-align: left;">Information</th>
<th style="text-align: right;">Price</th>
</tr>
<?
$i=0;
while ($i < $num) {
$company1=mysql_result($result,$i,"company1");
$information=mysql_result($result,$i,"information");
$price=mysql_result($result,$i,"price");
?>
<tr>
<td><? echo "$company1"; ?></td>
<td><? echo "$information"; ?></td>
<td><? echo "$price"; ?></td>
</tr>
<?
++$i;
}
echo "</table>";
?>