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
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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.