Jump to content


Photo

Array Help Needed?


  • Please log in to reply
7 replies to this topic

#1 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 01:55 AM

I'm trying to assign values to each Delete Entry link. But i'm having some trouble doing it. Here is my 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;
	}
	?>


#2 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 02:29 AM

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:

<?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>";
?>

For personal preference I would have written this like so:
<?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";
?>

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

#3 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 02:40 AM

THis is the error i get

Parse error: parse error, unexpected T_WHILE in /home/totallyc/public_html/login/blog.php on line 37


Line 37 is :

while($row=mysql_fetch_array($result)) {



#4 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 02:44 AM

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

#5 marknt

marknt
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts
  • LocationPhilippines

Posted 02 October 2006 - 02:47 AM

THis is the error i get

Parse error: parse error, unexpected T_WHILE in /home/totallyc/public_html/login/blog.php on line 37


Line 37 is :

while($row=mysql_fetch_array($result)) {

i may add
while($row=mysql_fetch_array($result, MYSQL_BOTH))


#6 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 02:50 AM

If i declare $x inside loop won't it reset x to 1 everytime?

#7 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 02:54 AM

I think you've misunderstood or I've not been clear...

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

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

#8 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 02 October 2006 - 03:12 AM

Oh oh.. I see. sorry for misunderstanding you. THanks for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users