Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
I'm sure there's a more efficient way to go about this.
Old 11-17-2008, 02:54 AM I'm sure there's a more efficient way to go about this.
gmcalp's Avatar
Novice Talker

Posts: 8
Name: Geoff
Trades: 0
Hi there, this is my first post here (though I've found several answers to previous problems here by searching).

I have a script that I wrote that is pulling data from a DB. My script works fine for pulling the data from the DB and displaying it, actually, the output is displayed correctly in Firefox (1.5 - 3.0.3) but there is a large gap between the Header (which is the job title) and the listing JobTitle when viewed in IE6 (I think it works fine in IE7 but since I am on Linux I can't check that right now - I'm looking for a fix on that issue..)

So, I'm looking for suggestions on how I can make this script more efficient. Here is the code:

PHP Code:
$result = @mysql_query("SELECT * FROM $db_table ORDER BY id DESC");
if (!
$result) {
   exit(
"<p>Error performing query: " .
       
mysql_error() . "</p>");
    } 
 
  elseif (
mysql_num_rows($result) == 0){
     echo 
"<h4>Sorry, there are no job listings at this time.</h4>";
    }     
 
  else {
    while (
$row mysql_fetch_array($result)) {
 
   
$id $row['id'];
   
$jobtitle $row['title'];
   
$empl $row['employer'];
   
$loc $row['location'];
   
$fte $row['fte'];
   
$shift $row['shift'];
   
$salary $row['salary'];
   
$desc $row['description'];
   
$quals $row['qualifications'];
   
$prefd $row['preferred'];
   
$howto $row['howtoapply'];
   
$url $row['url'];
   
$contName $row['contactname']; 
   
$contPhone $row['contactphone'];
   
$contEmail $row['contactemail'];
   
$added date("m/d/y"strtotime($row['added']));
 
     
// change all HTML special characters,
     // to prevent some nasty code injection
        
$desc htmlspecialchars($desc);
        
$quals htmlspecialchars($quals);
        
$prefd htmlspecialchars($prefd);         
        
$howto htmlspecialchars($howto);
 
     
// convert newline characters to HTML break tag ( <br /> )
        
$desc nl2br($desc);
        
$quals nl2br($quals);
        
$prefd nl2br($prefd);
        
$howto nl2br($howto);
 
    echo 
"<h3>".$jobtitle."</h3>"."\n";
   
//table format
    
echo "<table width=\"600\" border=\"0\" cellspacing=\"3\" cellpadding=\"3\" class=\"jobs\" summary=\"This table lists all of the available jobs listed with the Washington State Biomedical Association in the Northwestern region of the United States.\">"."\n";
    echo 
"<tr class=\"jobs\" valign=\"top\"><td width=\"110\"><strong>Title:</strong></td><td width=\"490\">".$jobtitle."</td></tr>"."\n";
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Employer:</strong></td><td class=\"jobs\">".$empl."</td></tr>"."\n";
 
if (
$loc !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Location:</strong></td><td class=\"jobs\">".$loc."</td></tr>"."\n";
    }
 
if (
$fte !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>FTE:</strong></td><td class=\"jobs\">".$fte."</td></tr>"."\n";
    }
 
if (
$shift !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Shift:</strong></td><td class=\"jobs\">".$shift."</td></tr>"."\n";
    }
 
if (
$salary !=''){     
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Salary:</strong></td><td class=\"jobs\">".$salary."</td></tr>"."\n";
    }
 
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Description:</strong></td><td class=\"jobs\">".$desc."</td></tr>"."\n";
 
if (
$quals !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Qualifications:</strong></td><td class=\"jobs\">" .$quals"</td></tr>"."\n";
    }
 
if (
$prefd !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Preferred:</strong></td><td class=\"jobs\">" .$prefd"</td></tr>"."\n";
    }
 
if (
$howto !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>How To Apply:</strong></td><td class=\"jobs\">".$howto."</td></tr>"."\n";
    }
 
if (
$url !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Website:</strong></td><td class=\"jobs\"><a href=\"http://".$url."\" target=\"_blank\" class=\"popup\">http://".$url."</a></td></tr>"."\n";
    }
 
if (
$contName !=''){     
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact Name:</strong></td><td class=\"jobs\">".$contName."</td></tr>"."\n";
    }
 
if (
$contPhone !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact Phone:</strong></td><td class=\"jobs\">".$contPhone."</td></tr>"."\n";
    }
 
if (
$contEmail !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact E-mail:</strong></td><td class=\"jobs\">".$contEmail."</td></tr>"."\n";
    }
 
    echo 
"</table>"."\n";
    echo 
"<div>(Added on: " .$added")</div>"."\n";
    echo 
"<br/><br/>"."\n";};
    };
 
mysql_close(); 
Here is the page with the actual output of the script:
http://www.bmet.org/employment.html

One thing in particular that I will be changing at some point is to automatically create the field names (Employer, Title, Salary, etc) by using the actual field names that are in the DB then displaying the other data in a pseudo table that uses the display: table CSS property.


So, any help from you guys will be much appreciated.
gmcalp is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-17-2008, 04:18 AM Re: I'm sure there's a more efficient way to go about this.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by gmcalp View Post
My script works fine for pulling the data from the DB and displaying it, actually, the output is displayed correctly in Firefox (1.5 - 3.0.3) but there is a large gap between the Header (which is the job title) and the listing JobTitle when viewed in IE6 (I think it works fine in IE7 but since I am on Linux I can't check that right now - I'm looking for a fix on that issue..)
If your problem is without your output is being displayed in IE6 then the problem is not with php but with your html. The html your code outputs along with any css would be more useful in solving this problem than your php source. From the code you posted I don't see anything that would cause any gaps.

PHP is server side code so it is browser independent. HTML and css are browser dependent.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-17-2008, 04:58 AM Re: I'm sure there's a more efficient way to go about this.
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Yes this would be better in CSS section.

There is a problem in IE6

maxxximus is offline
Reply With Quote
View Public Profile
 
Old 11-17-2008, 05:02 AM Re: I'm sure there's a more efficient way to go about this.
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Looks to me like a CSS/HTML error.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 11-17-2008, 10:36 AM Re: I'm sure there's a more efficient way to go about this.
gmcalp's Avatar
Novice Talker

Posts: 8
Name: Geoff
Trades: 0
Thanks, I'll have to take a look at my CSS, but the display issue was secondary to my question about the PHP code...

My question was is there a more efficient way to code what I have instead of having to do all those conditional "if" statements (in this section):

PHP Code:
echo "<table width=\"600\" border=\"0\" cellspacing=\"3\" cellpadding=\"3\" class=\"jobs\" summary=\"This table lists all of the available jobs listed with the Washington State Biomedical Association in the Northwestern region of the United States.\">"."\n";
    echo 
"<tr class=\"jobs\" valign=\"top\"><td width=\"110\"><strong>Title:</strong></td><td width=\"490\">".$jobtitle."</td></tr>"."\n";
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Employer:</strong></td><td class=\"jobs\">".$empl."</td></tr>"."\n";
 
if (
$loc !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Location:</strong></td><td class=\"jobs\">".$loc."</td></tr>"."\n";
    }
 
if (
$fte !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>FTE:</strong></td><td class=\"jobs\">".$fte."</td></tr>"."\n";
    }
 
if (
$shift !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Shift:</strong></td><td class=\"jobs\">".$shift."</td></tr>"."\n";
    }
 
if (
$salary !=''){     
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Salary:</strong></td><td class=\"jobs\">".$salary."</td></tr>"."\n";
    }
 
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Description:</strong></td><td class=\"jobs\">".$desc."</td></tr>"."\n";
 
if (
$quals !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Qualifications:</strong></td><td class=\"jobs\">" .$quals"</td></tr>"."\n";
    }
 
if (
$prefd !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Preferred:</strong></td><td class=\"jobs\">" .$prefd"</td></tr>"."\n";
    }
 
if (
$howto !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>How To Apply:</strong></td><td class=\"jobs\">".$howto."</td></tr>"."\n";
    }
 
if (
$url !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Website:</strong></td><td class=\"jobs\"><a href=\"http://".$url."\" target=\"_blank\" class=\"popup\">http://".$url."</a></td></tr>"."\n";
    }
 
if (
$contName !=''){     
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact Name:</strong></td><td class=\"jobs\">".$contName."</td></tr>"."\n";
    }
 
if (
$contPhone !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact Phone:</strong></td><td class=\"jobs\">".$contPhone."</td></tr>"."\n";
    }
 
if (
$contEmail !=''){
    echo 
"<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>Contact E-mail:</strong></td><td class=\"jobs\">".$contEmail."</td></tr>"."\n";
    }
 
    echo 
"</table>"."\n";
    echo 
"<div>(Added on: " .$added")</div>"."\n";
    echo 
"<br/><br/>"."\n";};
    };
 
mysql_close(); 
I would like to streamline my code and I'm asking for some suggestions on how to do that.
gmcalp is offline
Reply With Quote
View Public Profile
 
Old 11-21-2008, 03:52 PM Re: I'm sure there's a more efficient way to go about this.
Junior Talker

Posts: 1
Trades: 0
Seems you have 11 rows of data that you want to check and then display if the checks pass. Just use an array. Kind of like this...

Code:
<?php
$display = array("Location:","FTE:","Shift:") // enter all titles
?>
Then grab all the data and put into another array

Code:
$check[0] = $loc; // or whatever is coming out of the database
$check[1] = $fte;
OK You get the idea there.....

Now since it looks like the formatting is the same just make a simple for next loop to check all variables in order and display them.

$cnt is the number of array items minus one.
Code:
for($i=0;$i<$cnt;$i++){
   if($check[$i] != ''){ 
       echo "<tr class=\"jobs\" valign=\"top\"><td class=\"jobs\"><strong>" . $display[$i] . "</strong></td><td class=\"jobs\">".$check[$i]."</td></tr>"."\n";
}
I think that gives you an idea.

Mike
Indagroove is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to I'm sure there's a more efficient way to go about this.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24395 seconds with 12 queries