/    Sign up×
Community /Pin to ProfileBookmark

display search results if they exist – trying to condense code

Hi all,

I have been playing around with the code below and it will display Empty-mediadb1 and Empty-company if the search term (e.g. 1:00-10) does not exist. If it does exist, it displays the value located in mediadb1 and company. This is exactly how i want it. The only problem is that I have over 300 records that i need to search for in the `like`field (e.g. 1:00-10, 1:10-20, 1:20-30, etc) and i don`t want to just duplicate the above code 300 times (1 time to display each record in mediadb1. I hope there is a better way where i can just list an array (or something similar) of all the 300 searches without having to duplicate the code 300 times. I hope someone can help. Thanks

<?

// search for 1:00-10
session_start();
include_once”config.php”;
$result = mysql_query( “SELECT * FROM user WHERE mediadb1 LIKE ‘1:00-10′” )
or die(“SELECT Error: “.mysql_error());
while ($row=mysql_fetch_array($result)) {
$reps.=$row[“mediadb1”];
$comp.=$row[“company”];
}
if($reps == NULL) {
$reps=’Empty-mediadb1′;
$comp=’Empty-company’;
}
else {
}

// search for 1:10-20
$result2 = mysql_query( “SELECT * FROM user WHERE mediadb1 LIKE ‘1:10-20′” )
or die(“SELECT Error: “.mysql_error());
while ($row2=mysql_fetch_array($result2)) {
$reps2.=$row2[“mediadb1”];
$comp2.=$row2[“company”];
}
if($reps2 == NULL) {
$reps2=’Empty-mediadb1′;
$comp2=’Empty-company’;
}
else {
}

?>
<table width=”400″ border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<!– display 1:00-10 –>
<td width=”100″ align=”right”>1:00-10</td><td width=”150″ align=”right”><?php print $reps?></td> <td width=”150″ align=”right”><?php print $comp?></td>
</tr>
<tr>
<!– display 1:10-20 –>
<td width=”100″ align=”right”>1:10-20</td><td width=”150″ align=”right”><?php print $reps2?></td> <td width=”150″ align=”right”><?php print $comp2?></td>
</tr>
</table>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@MGenevAug 22.2010 — I'm assuming you have a table containing all the "1:00-10" etc. data (in the example I've named this table mediadb and the column is mediadb1). So, did you mean something like that:

[code=php]
<?php

session_start();
include_once "config.php";

$query = <<<EOT
SELECT m.mediadb1 AS mmediadb,
IFNULL(u.mediadb1, 'Empty-mediadb1') AS umediadb,
IFNULL(u.company, 'Empty-company') AS ucompany
FROM user AS u
RIGHT JOIN mediadb AS m ON u.mediadb1 = m.mediadb1
WHERE m.mediadb1 IS NOT NULL
ORDER BY CAST(REPLACE(REPLACE(m.mediadb1, '-', ''), ':', '') AS INT)
EOT;

$result = mysql_query($query) or die('Query error.');

echo '<table width="400" border="0" cellspacing="0" cellpadding="0" align="center">' . PHP_EOL;

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

echo <<<EOT
<tr>
<td width="100" align="right">{$row['mmediadb']}</td>
<td width="150" align="right">{$row['umediadb']}</td>
<td width="150" align="right">{$row['ucompany']}</td>
</tr>
EOT;

}

echo '</table>';

?>
[/code]
×

Success!

Help @cedric813 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...