Jump to content

variables, loops, and $i=-1


wright67uk

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/233635-variables-loops-and-i-1/
Share on other sites

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!

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.

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

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.