alexcrosson Posted October 2, 2006 Share Posted October 2, 2006 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] Quote Link to comment Share on other sites More sharing options...
Destruction Posted October 2, 2006 Share Posted October 2, 2006 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]<?phpecho "<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]<?phpecho "<div id=\"blog\">\n" ."<h1>{$row['title']}</h1>\n" ."<p>{$row['content']}</p><br/>\n" ."<h2>Edit Entry | <a href=\"?a=delete&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 Quote Link to comment Share on other sites More sharing options...
alexcrosson Posted October 2, 2006 Author Share Posted October 2, 2006 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 Link to comment Share on other sites More sharing options...
Destruction Posted October 2, 2006 Share Posted October 2, 2006 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.HTHDest Quote Link to comment Share on other sites More sharing options...
marknt Posted October 2, 2006 Share Posted October 2, 2006 [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] Quote Link to comment Share on other sites More sharing options...
alexcrosson Posted October 2, 2006 Author Share Posted October 2, 2006 If i declare $x inside loop won't it reset x to 1 everytime? Quote Link to comment Share on other sites More sharing options...
Destruction Posted October 2, 2006 Share Posted October 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
alexcrosson Posted October 2, 2006 Author Share Posted October 2, 2006 Oh oh.. I see. sorry for misunderstanding you. THanks for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.