a little help with php please

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
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

a little help with php please

Post 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
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: a little help with php please

Post by twinedev »

You are toggling colors based upon $row_count however in the loop you are incrementing $count

-Greg
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: a little help with php please

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

Re: a little help with php please

Post 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?
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

Re: a little help with php please

Post 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
vegas22
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2010 12:00 pm

Re: a little help with php please

Post by vegas22 »

sussed it thanks guys
Post Reply