Jump to content

Archived

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

alexcrosson

Array Help Needed?

Recommended Posts

I'm trying to assign values to each Delete Entry link. But i'm having some trouble doing it. Here is my code:

[code]  <?php
  switch($_GET['a']) {
case "add_entry":
  include("add_entry.php");
break;
default:
  $result = mysql_query("SELECT * FROM blog ORDER BY date DESC") or die(mysql_error());
$x = 1
while($row=mysql_fetch_array($result)) {
$blog_array[$x] = $x
echo "<div id='blog'><h1>". $row['title']. "</h1><p>". $row['content']. "</p>". "<br /><h2>Edit Entry | <a href="?a=delete?d=$x">Delete Entry</a>". "</h2></div>";
$x = $x + 1
}
break;
}
?>[/code]

Share this post


Link to post
Share on other sites
Not sure why you have $blog_array a) inside the loop but undeclared outside and b) there but not being used.  Also, is there a particular issue you're having or error you're getting?  The above is a little vague and we're left guessing at exactly what's wrong by fixing up any hole we can find in the above rather than focussing on the actual problem at hand.  If you're getting a particular error please state what it is.  Also, you're not ending your lines with the ; terminator which usually gives me trouble in scripts if it's left out.  Also, the output being echo'd is somewhat sporadic instead of following a set pattern it seems:

[code]
<?php
echo "<div id='blog'><h1>". $row['title']. "</h1><p>". $row['content']. "</p>". "<br /><h2>Edit Entry | <a href="?a=delete?d=$x">Delete Entry</a>". "</h2></div>";
?>
[/code]

For personal preference I would have written this like so:
[code]
<?php
echo "<div id=\"blog\">\n"
      ."<h1>{$row['title']}</h1>\n"
      ."<p>{$row['content']}</p><br/>\n"
      ."<h2>Edit Entry | <a href=\"?a=delete&amp;d=$x\">Delete Entry</a></h2>\n"
      ."</div>\n";
?>
[/code]

However, that's because that reads more clearly to me when I'm scanning through my scripts looking for elusive errors.  Each person develops their own style but the key word is consistency.  Also in the above, note the escaping of the double quotes on your link for edit/delete.  As these weren't escaped they may have presented a part of the problem. 

Also, instead of $x = $x + 1; you can use an incrementer such as: $x++; which will do the same job. 

Hope this helps,

Dest

Share this post


Link to post
Share on other sites
THis is the error i get
[b]
Parse error: parse error, unexpected T_WHILE in /home/totallyc/public_html/login/blog.php on line 37[/b]

Line 37 is : [quote]while($row=mysql_fetch_array($result)) {[/quote]

Share this post


Link to post
Share on other sites
That's because you missed out the ; on $x = 1;

If you get an error "Unexpected" anything, it's usually the lines before it that are at fault...most often something is missing that's needed before it can do the next line which in this case is the while loop.

HTH

Dest

Share this post


Link to post
Share on other sites
[quote author=alexcrosson link=topic=110197.msg444987#msg444987 date=1159756805]
THis is the error i get
[b]
Parse error: parse error, unexpected T_WHILE in /home/totallyc/public_html/login/blog.php on line 37[/b]

Line 37 is : [quote]while($row=mysql_fetch_array($result)) {[/quote]
[/quote]
i may add
[code]while($row=mysql_fetch_array($result, MYSQL_BOTH))[/code]

Share this post


Link to post
Share on other sites
I think you've misunderstood or I've not been clear...

[code]
<?php
$x = 1;
while(whatever)
{
//do something
$x++;
}
?>
[/code]

Will set $x to 1 initially, run the loop, and on each loop increment (increase) the value of $x by 1.

I hope this is clearer.

Dest

Share this post


Link to post
Share on other sites

×

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.