Page 1 of 1

a little help with php please

Posted: Fri Oct 29, 2010 12:02 pm
by vegas22
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.

Code: Select all

<?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 "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }

// 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 "&nbsp;<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! :)

thanks in advance

Re: a little help with php please

Posted: Fri Oct 29, 2010 12:10 pm
by twinedev
You are toggling colors based upon $row_count however in the loop you are incrementing $count

-Greg

Re: a little help with php please

Posted: Fri Oct 29, 2010 12:12 pm
by pickle
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:

Code: Select all

$row_color = '#CCFFCC';
while(some stuff here)
{
    $row_color = ($row_color == '#CCFFCC') ? '#BFD8BC' : '#CCFFCC';
    //rest of your code here
} 
Also, look into the heredoc syntax. It's much nicer to use when doing multi-line output.

Re: a little help with php please

Posted: Fri Oct 29, 2010 12:33 pm
by vegas22
thanks for your help, im playing about with the code but to be honest im struggling. can you give me a bit more guidance or code example?

Re: a little help with php please

Posted: Fri Oct 29, 2010 12:39 pm
by vegas22
this is what i have now but still no job.

Code: Select all

<?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 "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }

// 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 "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }}


?>
thanks again

Re: a little help with php please

Posted: Fri Oct 29, 2010 12:44 pm
by vegas22
sussed it thanks guys