wright67uk Posted April 13, 2011 Share Posted April 13, 2011 Hello, can anybody help me with this question about variables within loops? $query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name"); echo mysql_error(); while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0]; $i = -1; foreach($nt as $value) {$i++; echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$filename'>" . $nt[$i] . "</a>" . "<br/>"; // LINE 7 $FileName = str_replace(' ','_',$nt[$i]) . ".php"; // LINE 8 $FileHandle = fopen($FileName, 'w') or die("cant open file"); $pageContents = file_get_contents("header.php"); fwrite($FileHandle,"$pageContents");} fclose($FileHandle); ?> header.php <p>HEADER UPDATED!</p> <p>the heading below should read (title goes here in capital letter)</p> <?php $title = $_GET['title']; $subtype = $_GET['subtype']; echo "<h1>$title</h1>"; echo "<h1>$subtype</h1>"; ?> When I echo $subtype I want subtype to be the file name of the page its displayed in. However thats not the case. Everypage I open is displaying the subtype of the previous page produced from my database. I assume that this is because $si = -1 is displayed before my loop. How can I get $filename in Line 7 to display the same as $filename in Line 8? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 13, 2011 Share Posted April 13, 2011 You've got 2 different variables there, $filename and $FileName. The lowercase one, $filename, doesn't appear to be set anywhere in your code. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 14, 2011 Author Share Posted April 14, 2011 Thankyou, this was a typo. The problem still exists when I change the variable back to FileName. Here is a few of the urls produced... ###.co.uk/Fried_Chicken.php?title=TITLE GOES HERE&subtype=chinese.php ###.co.uk/thai_cusine.php?title=TITLE GOES HERE&subtype=Fried_Chicken.php ###.co.uk/bigmacs.php?title=TITLE GOES HERE&subtype=thai_cusine.php ###.co.uk/Indian.php?title=TITLE GOES HERE&subtype=bigmacs.php ###.co.uk/takeout_pasta.php?title=TITLE GOES HERE&subtype=Indian.php ###.co.uk/mexican.php?title=TITLE GOES HERE&subtype=takeout_pasta.php ###.1pw.co.uk/snails.php?title=TITLE GOES HERE&subtype=mexican.php As you will see each url shows the variable (subtype) as the filename of the page above it. For example the 1st URL should be reading; ###.co.uk/Fried_Chicken.php?title=TITLE GOES HERE&subtype=Fried_Chicken.php Im a bit confused! Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 14, 2011 Author Share Posted April 14, 2011 Is this more likely to be a php coding issue or would it be the way ive setup mysql database? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 14, 2011 Share Posted April 14, 2011 Ok now that the typo is fixed, could the problem be that your order of operations is this: 1. Increment $i 2. echo $nt[$i] and $FileName 3. Set $FileName from $nt[$i] The problem being that you set the filename after displaying it, not before. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted April 14, 2011 Author Share Posted April 14, 2011 Yes I believe so. I got the code below to work in the end; $query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name"); echo mysql_error(); while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0]; $i = -1; foreach($nt as $value) {$i++; $FileName = str_replace(' ','_',$nt[$i]) . ".php"; $FileUsed = str_replace('_',' ',$nt[$i]); echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>" . $nt[$i] . "</a>" . "<br/>"; $FileHandle = fopen($FileName, 'w') or die("cant open file"); $pageContents = file_get_contents("header.php"); fwrite($FileHandle,"$pageContents");} fclose($FileHandle); Php is great I didnt know how much you could do with it. Now for the clean up lol Quote Link to comment Share on other sites More sharing options...
btherl Posted April 14, 2011 Share Posted April 14, 2011 Can I suggest a few changes: $query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Restaurant' ORDER BY name"); if (!$query) { # Change 1 - Only echo error if query failed, and exit script if query fails. echo mysql_error(); exit(1); } while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0]; $i = -1; foreach($nt as $value) { # Change 2 - indent contents of foreach loop, and put { and } on their own lines $i++; $FileName = str_replace(' ','_',$nt[$i]) . ".php"; $FileUsed = str_replace('_',' ',$nt[$i]); # Change 3 - $FileName can be used below instead of repeating the str_replace() echo "<a href='$FileName?title=$title&subtype=$FileUsed'>" . $nt[$i] . "</a>" . "<br/>"; $FileHandle = fopen($FileName, 'w') or die("cant open file"); $pageContents = file_get_contents("header.php"); fwrite($FileHandle,"$pageContents"); } fclose($FileHandle); The changes are to improve error handling (stop the script if query fails), readability (make it clear what's in the loop and what's not) and to remove duplicated code (the str_replace). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.