Jump to content

Array Help Needed?


alexcrosson

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]
Link to comment
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
Link to comment
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]
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.