/    Sign up×
Community /Pin to ProfileBookmark

Red headed step child

Ok, no offense to redheads or stepchildren…needed a catchy title. I am new to PHP, have code that “works”, but it sure looks inefficient to me. anyone know of a better way to handle the repetitive IF Else statements?

Thanks in advance.

Here’s the code.

<?php
// Make a MySQL Connection

// Get all the data from the “members” table
$result = mysql_query(“SELECT * FROM members ORDER BY Utility ASC,Person ASC”)
or die(mysql_error());

echo “<table border=’1′>”;
echo “<tr> <th>First Name</th> <th>Last Name</th> <th>Title</th> <th>Email</th> <th>Company</th></tr>”;
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

if ($row[‘Utility’]==”1″) {
echo “Utility Name 1”;
} else {
if ($row[‘Utility’]==”2″) {
echo “Utility Name 2”;
} else {
if ($row[‘Utility’]==”3″) {
echo “Utility Name 3”;
} else {
if ($row[‘Utility’]==”4″) {
echo “Utility Name 4”;
} else {
if ($row[‘Utility’]==”5″) {
echo “Utility Name 5”;
} else {
if ($row[‘Utility’]==”6″) {
echo “Utility Name 6”;
} else {
if ($row[‘Utility’]==”7″) {
echo “Utility Name 7”;
} else {
if ($row[‘Utility’]==”8″) {
echo “Utility Name 8”;
} else {
if ($row[‘Utility’]==”9″) {
echo “Utility Name 9”;
} else {
if ($row[‘Utility’]==”10″) {
echo “Utility Name 10”;
} else {
if ($row[‘Utility’]==”11″) {
echo “Utility Name 11”;
} else {
if ($row[‘Utility’]==”12″) {
echo “Utility Name 12”;
} else {
if ($row[‘Utility’]==”13″) {
echo “Utility Name 13”;
} else {
if ($row[‘Utility’]==”14″) {
echo “Utility Name 14”;
} else {
if ($row[‘Utility’]==”15″) {
echo “Utility Name 15”;
} else {
if ($row[‘Utility’]==”16″) {
echo “Utility Name 16”;
} else {
if ($row[‘Utility’]==”17″) {
echo “Utility Name 17”;
} else {
if ($row[‘Utility’]==”18″) {
echo “Utility Name 18”;
} else {
if ($row[‘Utility’]==”19″) {
echo “Utility Name 19”;
} else {
if ($row[‘Utility’]==”20″) {
echo “Utility Name 20”;
} else {
if ($row[‘Utility’]==”21″) {
echo “Utility Name 21”;
} else {
if ($row[‘Utility’]==”22″) {
echo “Utility Name 22”;
} else {
if ($row[‘Utility’]==”23″) {
echo “Utility Name 23”;
} else {
if ($row[‘Utility’]==”24″) {
echo “Utility Name 24”;
} else {
if ($row[‘Utility’]==”25″) {
echo “Utility Name 25”;
} else {
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
echo “<br>”;

if ($row[‘JobCategory’]==”presceo”) {
echo “President and CEO”;
} else {
if ($row[‘JobCategory’]==”coo”) {
echo “Chief Operating Officer”;
} else {
if ($row[‘JobCategory’]==”cfo”) {
echo “Top Financial Executive”;
} else {
if ($row[‘JobCategory’]==”legal”) {
echo “Top Legal Executive”;
} else {
if ($row[‘JobCategory’]==”nuclear”) {
echo “Top Nuclear Executive”;
} else {
if ($row[‘JobCategory’]==”generation”) {
echo “Top Power Generation Executive–non-nuclear”;
} else {
if ($row[‘JobCategory’]==”distribution”) {
echo “Top Distribution Executive”;
} else {
if ($row[‘JobCategory’]==”transmission”) {
echo “Top Transmission Systems Executive”;
} else {
if ($row[‘JobCategory’]==”energymarketing”) {
echo “Top Energy Marketing Executive”;
} else {
if ($row[‘JobCategory’]==”strategicplanning”) {
echo “Top Strategic Planning Executive”;
} else {
if ($row[‘JobCategory’]==”communications”) {
echo “Top Communications Executive”;
} else {
if ($row[‘JobCategory’]==”custservice”) {
echo “Top Customer Service Executive”;
} else {
if ($row[‘JobCategory’]==”cio”) {
echo “Information Technology and Services–CIO”;
} else {
if ($row[‘JobCategory’]==”financial”) {
echo “Finance Accounting and Information Services”;
} else {
if ($row[‘JobCategory’]==”hr”) {
echo “Human Resources”;
} else {
if ($row[‘JobCategory’]==”gr”) {
echo “Government Relations”;
} else {
if ($row[‘JobCategory’]==”safety”) {
echo “Safety”;
} else {
if ($row[‘JobCategory’]==”gasutility”) {
echo “Gas Utility Executive”;
} else {
if ($row[‘JobCategory’]==”water”) {
echo “Water Executive”;
} else {
if ($row[‘JobCategory’]==”wholesale”) {
echo “Wholesale Executive”;
} else {
if ($row[‘JobCategory’]==”retailsales”) {
echo “Retail Sales Executive”;
} else {
if ($row[‘JobCategory’]==”corpplanning”) {
echo “Corporate Planning Executive”;
} else {
if ($row[‘JobCategory’]==”utilitiesservices”) {
echo “Utilities Services Executive”;
} else {
if ($row[‘JobCategory’]==”marketing”) {
echo “Marketing Executive”;
} else {
if ($row[‘JobCategory’]==”electric”) {
echo “Electric Department Executive”;
} else {
if ($row[‘JobCategory’]==”environmental”) {
echo “Top Environmental Affairs Executive”;
} else {
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
echo “<br>”;
echo $row[‘FirstName’];
echo ” “;
echo $row[‘LastName’];
echo “, “;
echo $row[‘Title’];
echo “<br>”;
echo “work: “;
echo $row[‘WorkPhone’];
echo “<br>”;
echo “cell: “;
echo $row[‘CellPhone’];
echo “<br>”;
echo “fax: “;
echo $row[‘Fax’];
echo “<br>”;
echo “email: “;
echo ‘<a href=”mailto:’ . $row[‘Email’] . ‘”>’ . $row[‘Email’] . ‘</a>’;
echo “<br><br><hr>”;
}

?>

to post a comment
PHP

23 Comments(s)

Copy linkTweet thisAlerts:
@007JulienOct 01.2011 — It could be better to build the answers
[code=php]
if (!empty($row['Utility'])) echo "Utility Name ".$row['Utility'];[/code]

Or to define an array
[code=php]
$jobCat=array();
$jobCat["presco"]="President and CEO";
$jobCat["coo"]="Chief Operating Officer";
$jobCat["cfo"]="Top Financial Executive";
$jobCat["legal"]="Top Legal Executive";
// ...
$jobCat["environmental"]="Top Environmental Affairs Executive";
[/code]

to write in an unique line
[code=php]
if (!empty($row['JobCategory'])) echo $jobCat[$row['JobCategory']];[/code]
Copy linkTweet thisAlerts:
@007JulienOct 01.2011 — It could be better to build the answers
[code=php]
if (!empty($row['Utility'])) echo "Utility Name ".$row['Utility'];[/code]

Or to define an array
[code=php]
$jobCat=array();
$jobCat["presco"]="President and CEO";
$jobCat["coo"]="Chief Operating Officer";
$jobCat["cfo"]="Top Financial Executive";
$jobCat["legal"]="Top Legal Executive";
// ...
$jobCat["environmental"]="Top Environmental Affairs Executive";
[/code]

to write in an unique line
[code=php]
if (!empty($row['JobCategory'])) echo $jobCat[$row['JobCategory']];[/code]
Copy linkTweet thisAlerts:
@fideltfgOct 01.2011 — two ways:


1 : use a switch instead of all the if else's statements. this works better if you need process some thing if the case is true and allows you to assign any number of cases to the same result.

2: (pretty much what 007Julien said) put all your values in an array, then do a simple echo like this

[code=php]
$jobCatArray = (
'presceo' => 'President and CEO',
'coo' =>'Chief Operating Officer',
'cfo' => 'Top Financial Executive'
);

echo $jobCatArray[$row['JobCategory']];
[/code]


this way is a lot faster to process and easier to read


May I also say that if you really need to do all the if else things then rather than doing this
[code=php]
if ($row['Utility']=="1") {
echo "Utility Name 1";
} else {
if ($row['Utility']=="2") {
echo "Utility Name 2";
} else {
if ($row['Utility']=="3") {
echo "Utility Name 3";
} else {
if ($row['Utility']=="4") {
echo "Utility Name 4";
} else {
if ($row['Utility']=="5") {
echo "Utility Name 5";
} else {
if ($row['Utility']=="6") {
echo "Utility Name 6";
} else {
if ($row['Utility']=="7") {
echo "Utility Name 7";
} else {
if ($row['Utility']=="8") {
echo "Utility Name 8";
} else {
...............
[/code]


you should do it like this
[code=php]

if ($row['Utility']=="1") {
echo "Utility Name 1";
} elseif ($row['Utility']=="2") {
echo "Utility Name 2";
} elseif ($row['Utility']=="3") {
echo "Utility Name 3";
} elseif ($row['Utility']=="4") {
echo "Utility Name 4";
} elseif ($row['Utility']=="5") {
echo "Utility Name 5";
} elseif ($row['Utility']=="6") {
echo "Utility Name 6";
} elseif ($row['Utility']=="7") {
echo "Utility Name 7";
} elseif ($row['Utility']=="8") {
echo "Utility Name 8";
} elseif .........[/code]

This second method equates effectively to a switch statement. Only its a lot harder to read and takes longer to code.

There is always a better way to do this then nesting if else statements. yes it works but it makes upkeep a pita and trying to find a missing }.......

there are time when you need to nest if's like you did, but only if you require the second if statement to be evaluated ONLY as part of the else statement. The way you have it each and every if has to be evaluated. its ok if the value is is found in the first few if's. but if the value is not matched until the end php has to try every one.and that waists a lot of processing cycles.
Copy linkTweet thisAlerts:
@criterion9Oct 02.2011 — two ways:


1 : use a switch instead of all the if else's statements. this works better if you need process some thing if the case is true and allows you to assign any number of cases to the same result.

2: (pretty much what 007Julien said) put all your values in an array, then do a simple echo like this

[code=php]
$jobCatArray = (
'presceo' => 'President and CEO',
'coo' =>'Chief Operating Officer',
'cfo' => 'Top Financial Executive'
);

echo $jobCatArray[$row['JobCategory']];
[/code]


this way is a lot faster to process and easier to read


May I also say that if you really need to do all the if else things then rather than doing this
[code=php]
if ($row['Utility']=="1") {
echo "Utility Name 1";
} else {
if ($row['Utility']=="2") {
echo "Utility Name 2";
} else {
if ($row['Utility']=="3") {
echo "Utility Name 3";
} else {
if ($row['Utility']=="4") {
echo "Utility Name 4";
} else {
if ($row['Utility']=="5") {
echo "Utility Name 5";
} else {
if ($row['Utility']=="6") {
echo "Utility Name 6";
} else {
if ($row['Utility']=="7") {
echo "Utility Name 7";
} else {
if ($row['Utility']=="8") {
echo "Utility Name 8";
} else {
...............
[/code]


you should do it like this
[code=php]

if ($row['Utility']=="1") {
echo "Utility Name 1";
} elseif ($row['Utility']=="2") {
echo "Utility Name 2";
} elseif ($row['Utility']=="3") {
echo "Utility Name 3";
} elseif ($row['Utility']=="4") {
echo "Utility Name 4";
} elseif ($row['Utility']=="5") {
echo "Utility Name 5";
} elseif ($row['Utility']=="6") {
echo "Utility Name 6";
} elseif ($row['Utility']=="7") {
echo "Utility Name 7";
} elseif ($row['Utility']=="8") {
echo "Utility Name 8";
} elseif .........[/code]

This second method equates effectively to a switch statement. Only its a lot harder to read and takes longer to code.

There is always a better way to do this then nesting if else statements. yes it works but it makes upkeep a pita and trying to find a missing }.......

there are time when you need to nest if's like you did, but only if you require the second if statement to be evaluated ONLY as part of the else statement. The way you have it each and every if has to be evaluated. its ok if the value is is found in the first few if's. but if the value is not matched until the end php has to try every one.and that waists a lot of processing cycles.[/QUOTE]

That whole mess of nested conditionals could be reduced to one statement:
[code=php]
echo "Utility Name ".$row['Utility'];
[/code]
Copy linkTweet thisAlerts:
@smelkinauthorOct 02.2011 — Thank you for the replies! I obviously "rigged" the code and got it working but I knew it was not pretty. I will work on this tomorrow and see how clean I can get the code while keeping it operational. Will let you know.

thx again all.
Copy linkTweet thisAlerts:
@smelkinauthorOct 04.2011 — Took a few days for me to get back to this, working on job cat portion then will get to Utility If/Then statements. So when I run this through PHP syntax validator, I get a double arrow error message...

Syntax error found: unexpected T_DOUBLE_ARROW on line 7 . Here is current code. I also tried a simple = sign, a double == etc. Not sure what the difference is or why => isn't working?



<?php

$jobCatArray = (

'presceo' =>'President and CEO',

'coo' =>'Chief Operating Officer',

'cfo' =>'Top Financial Executive',

'legal' =>'Top Legal Executive',

'nuclear' =>'Top Power Generation Executive--non-nuclear',

'generation' =>'Top',

'distribution' =>'Top Distribution Executive',

'transmission' =>'Top Transmission Systems Executive',

'energymarketing' =>'Top Energy Marketing Executive',

'strategicplanning' =>'Top Strategic Planning Executive',

'communications' =>'Top Communications Executive',

'custservice' =>'Top Customer Service Executive',

'cio' =>'Information Technology and Services--CIO',

'financial' =>'Finance Accounting and Information Services',

'hr' =>'Human Resources',

'gr' =>'Government Relations',

'safety' =>'Safety',

'gasutility' =>'Gas Utility Executive',

'water' =>'Water Executive',

'wholesale' =>'Wholesale Executive',

'retailsales' =>'Retail Sales Executive',

'corpplanning' =>'Corporate Planning Executive',

'utilitiesservices' =>'Utilities Services Executive',

'marketing' =>'Marketing Executive',

'electric' =>'Electric Department Executive',

'environmental' =>'Top Environmental Affairs Executive'

);



// Get all the data from the "members" table

$result = mysql_query("SELECT * FROM members ORDER BY Utility ASC,Person ASC")

or die(mysql_error());

// keeps getting the next row until there are no more to get

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

// Print out the contents of each row into a table

echo "<b>";

if ($row['Utility']=="1") {

echo "Utility Name 1";

} else {

if ($row['Utility']=="2") {

echo "Utility Name 2";

} else {

if ($row['Utility']=="3") {

echo "Utility Name 3";

} else {

if ($row['Utility']=="4") {

echo "Utility Name 4";

} else {

if ($row['Utility']=="5") {

echo "Utility Name 5";

} else {

if ($row['Utility']=="6") {

echo "Utility Name 6";

} else {

if ($row['Utility']=="7") {

echo "Utility Name 7";

} else {

if ($row['Utility']=="8") {

echo "Utility Name 8";

} else {

if ($row['Utility']=="9") {

echo "Utility Name 9";

} else {

if ($row['Utility']=="10") {

echo "Utility Name 10";

} else {

if ($row['Utility']=="11") {

echo "Utility Name 11";

} else {

if ($row['Utility']=="12") {

echo "Utility Name 12";

} else {

if ($row['Utility']=="13") {

echo "Utility Name 13";

} else {

if ($row['Utility']=="14") {

echo "Utility Name 14";

} else {

if ($row['Utility']=="15") {

echo "Utility Name 15";

} else {

if ($row['Utility']=="16") {

echo "Utility Name 16";

} else {

if ($row['Utility']=="17") {

echo "Utility Name 17";

} else {

if ($row['Utility']=="18") {

echo "Utility Name 18";

} else {

if ($row['Utility']=="19") {

echo "Utility Name 19";

} else {

if ($row['Utility']=="20") {

echo "Utility Name 20";

} else {

if ($row['Utility']=="21") {

echo "Utility Name 21";

} else {

if ($row['Utility']=="22") {

echo "Utility Name 22";

} else {

if ($row['Utility']=="23") {

echo "Utility Name 23";

} else {

if ($row['Utility']=="24") {

echo "Utility Name 24";

} else {

if ($row['Utility']=="25") {

echo "Utility Name 25";

} else {

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

echo "</b>";

echo "<br>";

echo $jobCatArray[$row['JobCategory']];

echo "<br>";

echo $row['FirstName'];

echo " ";

echo $row['LastName'];

echo ", ";

echo $row['Title'];

echo "<br>";

echo "work: ";

echo $row['WorkPhone'];

echo "<br>";

echo "cell: ";

echo $row['CellPhone'];

echo "<br>";

echo "fax: ";

echo $row['Fax'];

echo "<br>";

echo "email: ";

echo '<a href="mailto:' . $row['Email'] . '">' . $row['Email'] . '</a>';

echo "<br><br><hr>";

}

?>
Copy linkTweet thisAlerts:
@fideltfgOct 05.2011 — replace [code=php]$jobCatArray = ([/code]with[code=php]$jobCatArray = array([/code]
Copy linkTweet thisAlerts:
@dalecospOct 05.2011 — That whole mess of nested conditionals could be reduced to one statement:
[code=php]
echo "Utility Name ".$row['Utility'];
[/code]
[/QUOTE]
Indeed, and nice. However, unless you're best buddies with every DBA who is likely to deal with your back-end (IOW, you *know* the field is set to only allow INT) ? :[code=php]if (is_numeric($row['Utility'])) {
echo "Utility Name ".$row['Utility'];
}[/code]
?
Copy linkTweet thisAlerts:
@smelkinauthorOct 05.2011 — replace [code=php]$jobCatArray = ([/code]with[code=php]$jobCatArray = array([/code][/QUOTE]

Thanks Fidel/all. I used two arrays and greatly reduced the code and server trips. One last question if I may, I will have 4 web pages that will use the same array, is it possible to make this php an external file call (like a script?)? And is this a good idea? Or is there some way to define this array globally?

Just curious. It would be even cleaner not repeating the same array.

Thanks again!
Copy linkTweet thisAlerts:
@dalecospOct 05.2011 — Yes, this is done frequently. Just include() a PHP file with the array defined in it - the array will be available in the global scope of any script in which it has been included.

The "good idea" question would be regarding security. If the file with the array defined is inside a web root and could be guessed from outside, someone else might be able to read/use it.

That may not be a concern. Of course, PHP can include files from [b]outside[/b] the webroot most of the time, in which case your array wouldn't be available to anyone else, in theory.
Copy linkTweet thisAlerts:
@smelkinauthorOct 05.2011 — the whole shebang will be in a pw protected directory.

So I have used the include for all four files, you all rock. My code is looking very good and I'm almost done. Been working on final problem all day... here it goes:

I wrote something in asp years ago that basically took a form with two drop down fields and then sent the results to a routing file which, depending on the four combinations possible, redirected to the appropriate results URL.

Here is what I am trying to accomplish in that routing file:

IF (Utility = All) & (JobCategory = All) then redirect to resultsall.php

If (Utility = All) & (JobCategory does not = All), then redirect to resultsjcvariable.php and pass through form field data

If (Utility does not = All) and (JobCategory = All), then redirect to resultsmemvariable.php and pass through form field data

If (Utility does not = All) and (JobCategory does not = All), then redirect to results2variables.php and pass through form field data


Below is what I have so ginned up so far for this routing file/method. Can't get around this error --> Syntax error found: unexpected T_STRING, expecting ',' or ';' on line 8 .

Aside from the the syntax error, do you think this will work/is a good approach?


<?php

# GRAB THE VARIABLES FROM THE FORM



IF ($_POST['Utility'] == "all")

{

IF ($_
POST['JobCategory'] == "all")

echo "<form name="form2" action="resultsall.php" method="post">";

ELSE

echo "<form name="form2" action="resultsjcvariable.php" method="post">";

}

ELSE

{

IF ($_POST['JobCategory'] == "all")

echo "<form name="form2" action="memvariable.php" method="post">";

ELSE

echo "<form name="form2" action="results2variables.php" method="post">";

}

echo "<input type="hidden" name="JobCategory" value="<?=$_POST['JobCategory'];?>"/">";

echo "<input type="hidden" name="Utility" value="<?=$_
POST['Utility'];?>"/>";

echo "<input type="submit" name="submit" value="submit"/>";

echo "</form>";


?>
Copy linkTweet thisAlerts:
@fideltfgOct 05.2011 — the idea is good, but try it like this....
[code=php]<?php

if ($_POST ['Utility'] == "all") {
if ($_POST ['JobCategory'] == "all") {
$includeFileName = 'resultsall.php';
} else {
$includeFileName = 'resultsjcvariable.php';
}
} else {
if ($_POST ['JobCategory'] == "all") {
$includeFileName = 'memvariable.php';
} else {
$includeFileName = 'results2variables.php';
}
}
?>
<form name='form2' action='<?=$includeFileName?>' method='post'><input
type='hidden' name='JobCategory' value='<?=$_POST ['JobCategory']?>'> <input
type='hidden' name='Utility' value='<?=$_POST [' Utility']?> /> <input
type='submit' name='submit' value='submit' /></form>[/code]


This way separates your html from your php a bit better and will make upkeep easier
Copy linkTweet thisAlerts:
@dalecospOct 05.2011 — [code=php]if ($foo) {
header("location: http://somewhere.com");
}[/code]
Just make sure you have no output before your header call.
Copy linkTweet thisAlerts:
@fideltfgOct 05.2011 — Ah sorry I missed a bit of your post. Dalecosp hit on one method of doing it,

I would rather include the required files instead like this,

[code=php]
if (isset ( $_REQUEST ['submit'] )) {
if (isset ( $_REQUEST ['Utility'] ) && $_REQUEST ['Utility'] == 'all') {
if (isset ( $_REQUEST ['JobCategory'] ) && $_REQUEST ['JobCategory'] == 'all') {
include ("resultsall.php");
} else {
include ("resultsjcvariable.php");
}
} else {
if (isset ( $_REQUEST ['JobCategory'] ) && $_REQUEST ['JobCategory'] == 'all') {
include ("memvariable.php");
} else {
include ("results2variables.php");
}
}
}else{....display the form
[/code]


then the included file has implicit access to all data submitted in the form without having to save it in the session or passing it to the new location in an other manner
Copy linkTweet thisAlerts:
@smelkinauthorOct 05.2011 — Both of these are interesting. I do not understand ISSET at all but I will research it a bit.

Forgive my ignorance, but I think either I am missing something here in my approach or am not adequately describing desired end results. I don't believe either of these will accomplish the goal and perhaps that begs a question about what exactly an include command does. I thought it meant that there was an execution (or merely inclusion) of outside code within the current page. I wish visitors to end/leave the routing file completely (response.redirect in asp) and have the visitor land on one of the four destination pages. These destination pages have lots of html formatting with just the small amount of php included. Some of that html is unique.

Maybe I am approaching this the wrong way using a routing file and should have a single page for results and then echo in PHP the distinct treatment for each page. It really is only a handful of lines that change in each of the four pages.

Thoughts here?

Here is the pertinent part of one of those pages.

...html above...

<h1>Interactive Database </h1>

<h2>Search Results:

Specific

Member Company &amp; Specific Position

</h2>

</font></b>

<font size="2" face="Arial">

<p><br></p>

<b><font color="#4AA848">——Category:

[COLOR="Red"]<?php

//Include the PS_Pagination class

include('ps_pagination.php');

include('job-utilities-arrays.php');





// Make a MySQL Connection



$conn = mysql_connect('localhost',etc.);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('lppcrud1_memberdb', $conn);
if(!$status) die("Failed to select database!");


// Get JC data from the "members" table


// get job category from the form

$JobCat = $_GET['JobCategory'];

$CoName = $_
GET['Utility'];

echo $JobCat;

echo "</b>";

echo "</font>";

echo "<br>";

echo "<br>";

$sql = "SELECT * FROM members WHERE (JobCategory = '$JobCat' AND Utility = '$CoName') ORDER BY Person ASC";

/*
* Create a PS_Pagination object
*
* $conn = MySQL connection object
* $sql = SQl Query to paginate
* 20 = Number of rows per page
* 5 = Number of links
* "param1=valu1&param2=value2" = You can append your own parameters to paginations links
*/
$pager = new PS_Pagination($conn, $sql, 20, 5, "JobCategory=$JobCat");

/*
* Enable debugging if you want o view query errors
*/
$pager->setDebug(true);

/*
* The paginate() function returns a mysql result set
* or false if no rows are returned by the query
*/
$rs = $pager->paginate();
if(!$rs) die(mysql_error());
while($row = mysql_fetch_assoc($rs)) {
echo "<b>";
echo $utilArray[$row['Utility']];
echo "</b>";
echo "<br>";
echo $row['FirstName'];
echo " ";
echo $row['LastName'];
echo ", ";
echo $row['Title'];
echo "<br>";
echo "work: ";
echo $row['WorkPhone'];
echo "<br>";
echo "cell: ";
echo $row['CellPhone'];
echo "<br>";
echo "fax: ";
echo $row['Fax'];
echo "<br>";
echo "email: ";
echo '<a href="mailto:' . $row['Email'] . '">' . $row['Email'] . '</a>';
echo "<br><br><hr>";


echo $row['image'],"<br />n";
}

//Display the full navigation in one go
echo $pager->renderFullNav();

echo "<br />n";

/*
* Or you can display the individual links for more
* control over HTML rendering.
*
*/

//Display the link to first page: First
// echo $pager->renderFirst();

//Display the link to previous page: <<
// echo $pager->renderPrev();

/*
* Display page links: 1 2 3
* $prefix = Will be prepended to the page link (optional)
* $suffix = Will be appended to the page link (optional)
*
*/
//echo $pager->renderNav('<span>', '</span>');

//Display the link to next page: >>
//echo $pager->renderNext();

//Display the link to last page: Last
//echo $pager->renderLast();

?>[/COLOR]


<p>&nbsp;</p>

</td>
</tr>
</div>
</div>
<!-- InstanceEndEditable -->
<div id="right">
<div id="flashcontent">
<h1>Oops!</h1>
<p>It looks like you don't have flash player 6 installed. <a href="http://get.adobe.com/flashplayer/" >Click here</a> to go to Macromedia download page. </p>

</div>

<div class="centerrightcaption">
<p>Did you Know?</p>
</div>
<div class="centerright">

script>

<p>&nbsp;</p>

</div>


</div>
<br class="clearfloat" />
<div id="column_bottom">&nbsp;</div>

</div>

<div id="footer">

/div>

</div>

</body>

<!-- InstanceEnd --></html>
Copy linkTweet thisAlerts:
@criterion9Oct 06.2011 — Both of these are interesting. I do not understand ISSET at all but I will research it a bit.

Forgive my ignorance, but I think either I am missing something here in my approach or am not adequately describing desired end results. I don't believe either of these will accomplish the goal and perhaps that begs a question about what exactly an include command does. I thought it meant that there was an execution (or merely inclusion) of outside code within the current page. I wish visitors to end/leave the routing file completely (response.redirect in asp) and have the visitor land on one of the four destination pages. These destination pages have lots of html formatting with just the small amount of php included. Some of that html is unique.

Maybe I am approaching this the wrong way using a routing file and should have a single page for results and then echo in PHP the distinct treatment for each page. It really is only a handful of lines that change in each of the four pages.

Thoughts here?

Here is the pertinent part of one of those pages.

...html above...

<h1>Interactive Database </h1>

<h2>Search Results:

Specific

Member Company &amp; Specific Position

</h2>

</font></b>

<font size="2" face="Arial">

<p><br></p>

<b><font color="#4AA848">——Category:

[COLOR="Red"]<?php

//Include the PS_Pagination class

include('ps_pagination.php');

include('job-utilities-arrays.php');





// Make a MySQL Connection



$conn = mysql_connect('localhost',etc.);
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('lppcrud1_memberdb', $conn);
if(!$status) die("Failed to select database!");


// Get JC data from the "members" table


// get job category from the form

$JobCat = $_GET['JobCategory'];

$CoName = $_
GET['Utility'];

echo $JobCat;

echo "</b>";

echo "</font>";

echo "<br>";

echo "<br>";

$sql = "SELECT * FROM members WHERE (JobCategory = '$JobCat' AND Utility = '$CoName') ORDER BY Person ASC";

/*
* Create a PS_Pagination object
*
* $conn = MySQL connection object
* $sql = SQl Query to paginate
* 20 = Number of rows per page
* 5 = Number of links
* "param1=valu1&param2=value2" = You can append your own parameters to paginations links
*/
$pager = new PS_Pagination($conn, $sql, 20, 5, "JobCategory=$JobCat");

/*
* Enable debugging if you want o view query errors
*/
$pager->setDebug(true);

/*
* The paginate() function returns a mysql result set
* or false if no rows are returned by the query
*/
$rs = $pager->paginate();
if(!$rs) die(mysql_error());
while($row = mysql_fetch_assoc($rs)) {
echo "<b>";
echo $utilArray[$row['Utility']];
echo "</b>";
echo "<br>";
echo $row['FirstName'];
echo " ";
echo $row['LastName'];
echo ", ";
echo $row['Title'];
echo "<br>";
echo "work: ";
echo $row['WorkPhone'];
echo "<br>";
echo "cell: ";
echo $row['CellPhone'];
echo "<br>";
echo "fax: ";
echo $row['Fax'];
echo "<br>";
echo "email: ";
echo '<a href="mailto:' . $row['Email'] . '">' . $row['Email'] . '</a>';
echo "<br><br><hr>";


echo $row['image'],"<br />n";
}

//Display the full navigation in one go
echo $pager->renderFullNav();

echo "<br />n";

/*
* Or you can display the individual links for more
* control over HTML rendering.
*
*/

//Display the link to first page: First
// echo $pager->renderFirst();

//Display the link to previous page: <<
// echo $pager->renderPrev();

/*
* Display page links: 1 2 3
* $prefix = Will be prepended to the page link (optional)
* $suffix = Will be appended to the page link (optional)
*
*/
//echo $pager->renderNav('<span>', '</span>');

//Display the link to next page: >>
//echo $pager->renderNext();

//Display the link to last page: Last
//echo $pager->renderLast();

?>[/COLOR]


<p>&nbsp;</p>

</td>
</tr>
</div>
</div>
<!-- InstanceEndEditable -->
<div id="right">
<div id="flashcontent">
<h1>Oops!</h1>
<p>It looks like you don't have flash player 6 installed. <a href="http://get.adobe.com/flashplayer/" >Click here</a> to go to Macromedia download page. </p>

</div>

<div class="centerrightcaption">
<p>Did you Know?</p>
</div>
<div class="centerright">

script>

<p>&nbsp;</p>

</div>


</div>
<br class="clearfloat" />
<div id="column_bottom">&nbsp;</div>

</div>

<div id="footer">

/div>

</div>

</body>

<!-- InstanceEnd --></html>[/QUOTE]


I usually have a single "results" page and handle the specific logic there. Especially if this will reduce the amount of duplicated code (both server-side and client-side). Additionally, I think the issue you may be having is that change the action attribute of the form tag based on the submitted form means that until the form is submitted you won't know which action to use. To make your solution work as designed you should probably be using a header() redirection.
Copy linkTweet thisAlerts:
@eval_BadCode_Oct 06.2011 — Wow that code makes me CRINGE:

[code=php]

<?php
// Make a MySQL Connection

// Get all the data from the "members" table
$result = mysql_query("SELECT * FROM members ORDER BY Utility ASC,Person ASC") or die(mysql_error());


echo "<table border='1'>";
echo "<tr> <th>First Name</th> <th>Last Name</th> <th>Title</th> <th>Email</th> <th>Company</th></tr>";
// keeps getting the next row until there are no more to get


while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
printf("Utility Name %d<br />", $row['Utility']);

#think you made this problem twice... use a SWITCH statement if you must.
#Ideally, in my book anyways, the next part is also stored in the database so
#you don't need to translate a code into text but can relate the code to text instead.

}
[/code]
Copy linkTweet thisAlerts:
@dalecospOct 06.2011 — Forgive my ignorance, but I think either I am missing something here in my approach or am not adequately describing desired end results. I don't believe either of these will accomplish the goal and perhaps that begs a question about what exactly an include command does. I thought it meant that there was an execution (or merely inclusion) of outside code within the current page.[/quote]
That is, pretty much, [b]exactly[/b] what [url=http://php.net/include]include()[/url] does.
I wish visitors to end/leave the routing file completely (response.redirect in asp) and have the visitor land on one of the four destination pages. These destination pages have lots of html formatting with just the small amount of php included. Some of that html is unique.

Maybe I am approaching this the wrong way using a routing file and should have a single page for results and then echo in PHP the distinct treatment for each page. It really is only a handful of lines that change in each of the four pages.

Thoughts here?[/quote]
I snuck the answer in a couple posts ago. You have a script (you call it a "routing file") which evaluates some conditions based on user action/input (I assume). [url=http://php.net/header]header()[/url] is your friend:[code=php]
if ($something) {
header("location: somewhere.php");
} elseif ($something_else) {
header("location: somewhere_else.php");
}[/code]
HTH,
Copy linkTweet thisAlerts:
@dalecospOct 06.2011 — Ooh, I might also suggest that people may "cringe" less at your code if you wrap it in &#91;code&#93; or &#91;php&#93; tags ...
Copy linkTweet thisAlerts:
@smelkinauthorOct 06.2011 — Dale/all,

thanks and sorry to make you cringe...that is why I am here, to accomplish the task and learn in the process.

Here is where I am now. I stuck with the redirect file that gets the two variable values, checks them and redirects to one of 4 destination URLs. It works on my browser : ) !!!

I'm getting better already, aren't I? Now know how to do arrays, learning syntax, defining variables at the top, etc. ... a long way from 25 nested IF THen Else statements in asp. : )

I wanted run code by you all again and ask if you see any problems with this.


<?php

$url = "http://urlhere.com/";

$passvar = ("?".($_POST ['JobCategory'])."&".($_POST ['Utility']));

if ($_POST ['Utility'] == "all")

{

if ($_
POST ['JobCategory'] == "all")

{

$url2 = "resultsall.php";

header("Location: $url$url2$passvar");

exit;

}

else

{

$url2 = "resultsjcvariable.php";

header("Location: $url$url2$passvar");

exit;

}

}

else

{

if ($_POST ['JobCategory'] == "all")

{

$url2 = "resultsmemvariable.php";

header("Location: $url$url2$passvar");

exit;

}

else

{

$url2 = "results2variables.php";

header("Location: $url$url2$passvar");

exit;

}

}

?>
Copy linkTweet thisAlerts:
@dalecospOct 06.2011 — I'm not sure your $passvar variable is quite right - perhaps this instead?[code=php]$passvar = ("?JobCategory=".($_POST ['JobCategory'])."&Utility=".($_POST ['Utility']));[/code]Also, you don't route them anywhere if there's an error condition, if the correct variables aren't set, etc. You could just output an error at the bottom of this script, I suppose, or send them to the error page. How you do that might depend on what sort of feedback you intend to give them.

And I forgot to mention, if the script is on the same site, you don't need the "http://somesite.com" part in your header; header("page.php?var=something"); should work.
Copy linkTweet thisAlerts:
@smelkinauthorOct 06.2011 — I'm not sure your $passvar variable is quite right - perhaps this instead?[code=php]$passvar = ("?JobCategory=".($_POST ['JobCategory'])."&Utility=".($_POST ['Utility']));[/code][/QUOTE]

Curious thing is, I just had "?JobCategory=" and "&Utility=" in there, but what it did was create a duplicate of the variable in the url for some reason. With your version for example:

results2variables.php?JobCategory=JobCategory=presceo&Utility=Utility=16

With my version:

results2variables.php?JobCategory=presceo&Utility=16

so I don't know why that is, but it works?
Copy linkTweet thisAlerts:
@dalecospOct 06.2011 — The only reason I could think of is that the variables aren't set to what you think they are, but I guess there could be some other explanation.

Here:[567] Thu 06.Oct.2011 13:06:57
[kadmin@freebsd-devel][~] cat foo
&lt;?php

$_POST['JobCategory']="all";
$_POST['Utility']="all";

$passvar = "http://someurl.com/page.php".("?JobCategory=".($_POST ['JobCategory'])."&amp;Utility=".($_POST ['Utility']));

echo $passvar;

?&gt;

[568] Thu 06.Oct.2011 13:06:59
[kadmin@freebsd-devel][~] php foo
http://someurl.com/page.php?JobCategory=all&amp;Utility=all
×

Success!

Help @smelkin 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...