Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Sep 19, 2005, 10:44   #1
jamesmiddz
SitePoint Member
 
jamesmiddz's Avatar
 
Join Date: Jun 2004
Location: UK
Posts: 17
Swapping strings and displaying in loops problems

Does anybody know how I could improve this script?



PHP Code:



<?php

DEFINE
('NUM_COLUMNS', 2);
DEFINE('TABLE_WIDTH', 400);
$record_number = 0;
$column_width = floor(TABLE_WIDTH / NUM_COLUMNS);

$sql = "SELECT * FROM countries WHERE id='$id' ";
$result = mysql_query($sql);

echo
'<table border="0" cellspacing="0" cellpadding="2" width="'. TABLE_WIDTH . '">';

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

for (
$i = 1; $i < 44; $i++) {
$varname = 'f' . $i;

if (
$varname =="f1") $runner = "Showers";
if (
$varname =="f2") $runner = "Toilet block";
if (
$varname =="f3") $runner = "Washing-up area";
if (
$varname =="f4") $runner = "Launderette";

......................
etc.................................

if (
$varname =="f43") $runner = "Tea & Coffee En-suite";


if (${
$varname} == "Y") {
if(
fmod($record_number, NUM_COLUMNS) == 0)
echo
'<tr>';

echo
'<td class="mentex" valign="top" width="' . $column_width . '">';

echo
'<img src="yes.gif" alt="Available Facilities" width="15" border="0" />';
echo
' ' . $runner . '';
echo
'</td>';


$record_number++;
if(
fmod($record_number, NUM_COLUMNS) == 0)

echo
'</tr>';
}
}}
echo
'</table>';
?>
As you can see, the WHILE loop has to do a lot of processing in order to output the correct discriptions for each 'f' (facility).

I know I've not been too economical with this one...please don't laugh too loudly at this feeble attempt.

All the best, James
jamesmiddz is offline   Reply With Quote
Old Sep 19, 2005, 13:31   #2
bkennedy
SitePoint Addict
 
bkennedy's Avatar
 
Join Date: Aug 2004
Location: michigan
Posts: 246
You could use a switch(), case() instead of 43 if()'s, and as long as you used "break;"s after each one, it could help your script run faster in some cases. Otherwise, that's really not too much to put inside of a while loop.

Also, if you want runner to be a list, you'll want to use if ($varname =="f2") $runner .= "Toilet block"; (notice the ".=") otherwise, unless I'm missing something, $runner will end up being "Tea & Coffee En-suite" every time.

What is the query returning? And where in the script do you utilize the results? I was expecting to see "$row['field_name']" in there somewhere.

Does this script work for you? If it does, use it. My only suggestion would be the first sentence I wrote. And I'm obviously missing something.
bkennedy is offline   Reply With Quote
Old Sep 19, 2005, 14:06   #3
paulyG
SitePoint Wizard
 
Join Date: Jan 2004
Location: 3rd rock from the sun
Posts: 1,005
Quote:
SELECT * FROM countries WHERE id='$id'
Your query is only bringing back one result - yeah?
Unless you have more than one country with the same ID? - unlikely, so make sure its a unique field in your database....

So why are you doing a "while"? Thats for looping thru a set, array of results.

Your query is only getting one result or, no result.
so use plain:

$row=mysql_f.......

Now, to make sure it brings back a result which is non zero....

Quote:
if($row=mysql_f.....){

... do your s***

}else{
echo "sorry, you must be a hacker, or you picked a country which doesnt exist";
}
As previous poster said, how do you get from $row to $variable?
paulyG is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
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
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 23:16.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved