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!
can anyone sus out why this is returning the same coloured table rows? im trying to get it so the search displayes alternate colurs for the table rows.
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
}
//connect to your database ** EDIT REQUIRED HERE **
include("connect.php");
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("web112-dfslki73") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from jobmodule where description like \"%$trimmed%\"
order by jobname"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_color = ($row_count % 2) ? $color1 : $color2;
// begin to show results set
$count = 1 + $s ;
echo "<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" >
<tr>
<td style=\"color:#FFF;\" >
Job Title
</td>
<td style=\"color:#FFF;\">
Contract
</td>
<td style=\"color:#FFF;\">
Location
</td>
<td style=\"color:#FFF;\">
Salary
</td>
<td style=\"color:#FFF;\">
Posted Date</td>
<td style=\"color:#FFF;\">
More Info</td>
</tr>
<tr>
<td colspan=\"6\" height=\"1\">
</td>
</tr>";
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$jobname = $row["jobname"];
$contract = $row["spare1"];
$location = $row["location"];
$salary = $row["salary"];
$postdate = $row["postdate"];
$id = $row["id"];
echo "
<tr>
<td bgcolor=\"$row_color\">
$jobname
</td>
<td>
$contract
</td>
<td>
$location
</td>
<td>
$salary
</td>
<td>
$postdate</td>
<td>
<a href=\"job.php?id=$id\">More Info</a></td>
</tr>
"
;
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
if ($numrows == 0)
{
echo "
<tr>
<td colspan=\"6\" height=\"1\">Sorry, your search: "" . $trimmed . "" returned zero results
</td>
</tr>
";
}
echo "</table><br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
if ($s>=1) { // bypass PREV link if s is 0
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}}
?>
im still new to all this so please keep all help im php for dummies form!
You're not recalculating $row_color on each iteration of the while() loop. You're also using both $row_count and $count, which may cause some problems.
An easy way to do alternate row colouring (also called zebra striping) is this:
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
}
//connect to your database ** EDIT REQUIRED HERE **
include("connect.php");
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("web112-dfslki73") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from jobmodule where description like \"%$trimmed%\"
order by jobname"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
$row_color = '#CCFFCC';
$row_color = ($row_color == '#CCFFCC') ? '#BFD8BC' : '#CCFFCC';
// begin to show results set
$count = 1 + $s ;
echo "<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" >
<tr>
<td style=\"color:#FFF;\" >
Job Title
</td>
<td style=\"color:#FFF;\">
Contract
</td>
<td style=\"color:#FFF;\">
Location
</td>
<td style=\"color:#FFF;\">
Salary
</td>
<td style=\"color:#FFF;\">
Posted Date</td>
<td style=\"color:#FFF;\">
More Info</td>
</tr>
<tr>
<td colspan=\"6\" height=\"1\">
</td>
</tr>";
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$jobname = $row["jobname"];
$contract = $row["spare1"];
$location = $row["location"];
$salary = $row["salary"];
$postdate = $row["postdate"];
$id = $row["id"];
echo "
<tr>
<td bgcolor=\"$row_color\">
$jobname
</td>
<td>
$contract
</td>
<td>
$location
</td>
<td>
$salary
</td>
<td>
$postdate</td>
<td>
<a href=\"job.php?id=$id\">More Info</a></td>
</tr>
"
;
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
if ($numrows == 0)
{
echo "
<tr>
<td colspan=\"6\" height=\"1\">Sorry, your search: "" . $trimmed . "" returned zero results
</td>
</tr>
";
}
echo "</table><br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
if ($s>=1) { // bypass PREV link if s is 0
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}}
?>