Cory94bailly Posted May 17, 2008 Share Posted May 17, 2008 <?php mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)) printNews($row1); while($row2 = mysql_fetch_array($result2)) printNews($row2); function printNews($news) { if ( mysql_num_rows($news) < 1 ) { echo "<h1>No news!</h1>"; } else { echo ' <table><tr> <td><h2><u>' . $news['title'] . '</u></h2></td> </tr><tr> <td>' . $news['news'] . '</td> </tr></table><br />'; } } ?> It's just not showing anything, anyone care to explain why? Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/ Share on other sites More sharing options...
LooieENG Posted May 17, 2008 Share Posted May 17, 2008 Place the function bit before you use it Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-543832 Share on other sites More sharing options...
Cory94bailly Posted May 17, 2008 Author Share Posted May 17, 2008 Place the function bit before you use it Same thing.. blank. <?php mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)) printNews($row1); while($row2 = mysql_fetch_array($result2)) printNews($row2); function printNews($news) { echo ' <table><tr> <td><h2><u>' . $news['title'] . '</u></h2></td> </tr><tr> <td>' . $news['news'] . '</td> </tr></table><br />'; if ( mysql_num_rows($news) < 1 ) { echo "<h1>No news!</h1>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-543833 Share on other sites More sharing options...
LooieENG Posted May 17, 2008 Share Posted May 17, 2008 <?php function printNews($news) { if ( mysql_num_rows($news) < 1 ) { echo "<h1>No news!</h1>"; } else { echo ' <table><tr> <td><h2><u>' . $news['title'] . '</u></h2></td> </tr><tr> <td>' . $news['news'] . '</td> </tr></table><br />'; } } mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)) printNews($row1); while($row2 = mysql_fetch_array($result2)) printNews($row2); ?> Like this? Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-543835 Share on other sites More sharing options...
Cory94bailly Posted May 17, 2008 Author Share Posted May 17, 2008 <?php function printNews($news) { if ( mysql_num_rows($news) < 1 ) { echo "<h1>No news!</h1>"; } else { echo ' <table><tr> <td><h2><u>' . $news['title'] . '</u></h2></td> </tr><tr> <td>' . $news['news'] . '</td> </tr></table><br />'; } } mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); while($row1 = mysql_fetch_array($result1)) printNews($row1); while($row2 = mysql_fetch_array($result2)) printNews($row2); ?> Like this? Same thing.. blank. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-543838 Share on other sites More sharing options...
corbin Posted May 18, 2008 Share Posted May 18, 2008 At the top, put: error_reporting(E_ALL); ini_set('display_errors', 'E_ALL'); and see if it says anything. If nothing shows up, then it's most likely a fatal error. If that's the case, try setting display_errors in php.ini, look for a compile error, or put echo statements throughout the code to see where it stops going. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-543966 Share on other sites More sharing options...
wildteen88 Posted May 18, 2008 Share Posted May 18, 2008 At the top, put: error_reporting(E_ALL); ini_set('display_errors', 'E_ALL'); ini_set('display_errors', 'E_ALL'); should be ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544079 Share on other sites More sharing options...
Cory94bailly Posted May 18, 2008 Author Share Posted May 18, 2008 If that's the case, try setting display_errors in php.ini, look for a compile error, or put echo statements throughout the code to see where it stops going. I put a few echos and it went all the way past the "No news" part... Yet, it didn't show "No news!!!"........ Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544192 Share on other sites More sharing options...
Cory94bailly Posted May 18, 2008 Author Share Posted May 18, 2008 WAIT! Weird. I posted some news and then, it said that there's no news... If I delete the news, it shows blank... Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544197 Share on other sites More sharing options...
Cory94bailly Posted May 18, 2008 Author Share Posted May 18, 2008 Hmm.. When I add news it displays the news, it says no news but it also gives me this: Notice: Undefined variable: result1 in /home/content/m/a/r/markbailly/html/fcs/news.php on line 72 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/m/a/r/markbailly/html/fcs/news.php on line 72 Line 72: if ( mysql_num_rows($result1) < 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544202 Share on other sites More sharing options...
corbin Posted May 18, 2008 Share Posted May 18, 2008 At the top, put: error_reporting(E_ALL); ini_set('display_errors', 'E_ALL'); ini_set('display_errors', 'E_ALL'); should be ini_set('display_errors', 'On'); Ahhhh right. I can't ever remember config directives ;p. Should've just known that one though, since it's simply do you want to show errors or not.... Anyway..... What you're passing to printNews() is already a row, so that num_rows check is kinda... unneeded, and might cause problems. That error means that result1 is not defined. Possibly it's out of the variable scope or been unset. Can we see more code around it? Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544255 Share on other sites More sharing options...
Cory94bailly Posted May 18, 2008 Author Share Posted May 18, 2008 That error means that result1 is not defined. Possibly it's out of the variable scope or been unset. Can we see more code around it? I posted the whole script in the main post... Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544311 Share on other sites More sharing options...
wildteen88 Posted May 18, 2008 Share Posted May 18, 2008 Try: <?php mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { echo <<<HTML <tr> <td><h2><u>{$news['title']}</u></h2></td> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); printNews($result2); ?> However a better suggestion would be to merge your news and stickynews tables into one seeing as though they are both basically the same. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544335 Share on other sites More sharing options...
Cory94bailly Posted May 18, 2008 Author Share Posted May 18, 2008 Try: <?php mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM stickynews"; $query2 = "SELECT * FROM news"; $result1 = mysql_query($query1) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { echo <<<HTML <tr> <td><h2><u>{$news['title']}</u></h2></td> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); printNews($result2); ?> However a better suggestion would be to merge your news and stickynews tables into one seeing as though they are both basically the same. I'll try this whenever my sister get off my computer But that would ruin the whole point of "Stickied" news.. I want it so like if we are having a special for something, it will show it at the top always and if we ever post "less important" news, it will go under it.. Also, I wonder if there's a way to get it to show "Sticky:" in front of it without making it too complicated... Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544424 Share on other sites More sharing options...
wildteen88 Posted May 18, 2008 Share Posted May 18, 2008 But that would ruin the whole point of "Stickied" news.. I want it so like if we are having a special for something, it will show it at the top always and if we ever post "less important" news, it will go under it.. No it wont. All you need to do is just add a new field in your news table call this sticky. Set this new field to an ENUM type with possible values of Y and N sticky ENUM('y','n') NULL DEFAULT 'n' What ever news feeds you want to be stickied just set the sticky field to y. Then to retrieve stickied news from your news table just run the following query: SELECT * FROM news WHERE sticky='y' For non-stickied news run: SELECT * FROM news WHERE sticky='n' It seem pointless having two tables for news. Also, I wonder if there's a way to get it to show "Sticky:" in front of it without making it too complicated... Change: function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { echo <<<HTML <tr> <td><h2><u>{$news['title']}</u></h2></td> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); to: function printNews($result, $sn=false) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { $sticky = ($sn) ? 'STICKY: ' : null; echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { echo <<<HTML <tr> <th>{$sticky}<u>{$news['title']}</u></th> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1, true); // sticky news Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544453 Share on other sites More sharing options...
Cory94bailly Posted May 19, 2008 Author Share Posted May 19, 2008 No it wont. All you need to do is just add a new field in your news table call this sticky. Set this new field to an ENUM type with possible values of Y and N sticky ENUM('y','n') NULL DEFAULT 'n' How do I do that from phpmyadmin? What SQL query do I run? Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-544532 Share on other sites More sharing options...
wildteen88 Posted May 19, 2008 Share Posted May 19, 2008 Login to phpmyadmin, select your database then your news table. Make sure the Structure tab is selected. Find where it says Add [1] field(s) (note [1] is a text box). At the end of that line select Go. In the Field box type sticky Choose ENUM from the Type menu. Enter 'y','n' in the Length/Values field Select null from the Null menu Enter n in the Default field Select Save button. You should have a new column called sticky in your news table. When you browse to the Browse tab you should see all current news stored in your news table will have a sticky column with the letter n present in that column. To skicky a news entry select a row and edit it, change the sticky field to the letter y Now use the following code for displaying your news: <?php mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); $query1 = "SELECT * FROM news ORDER BY id, sticky ASC"; $result1 = mysql_query($query1) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { $sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : null; echo <<<HTML <tr> <th>{$sticky}<u>{$news['title']}</u></th> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); // none sticky news ?> EDIT: I posted the old code by mistake. New code above ^^ Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545024 Share on other sites More sharing options...
Cory94bailly Posted May 19, 2008 Author Share Posted May 19, 2008 Did that and I get this: Unknown column 'id' in 'order clause' Also, what do I do in my admin part to work with this? Admin part: <?php require('config.php'); // Make a MySQL Connection mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if(isset($_POST['postnews'])) { $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); // Insert the news $query = mysql_query("INSERT INTO news (title, news) VALUES ('$title', '$news')")or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> News posted!"; } elseif(isset($_POST['poststicky'])) { $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); // Insert the news $query = mysql_query("INSERT INTO stickynews (title, news) VALUES ('$title', '$news')")or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> <b>Stickied</b> news posted!</a>"; } elseif(isset($_POST['delete'])) { // Delete it all! mysql_query("TRUNCATE TABLE news")or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> All news deleted!"; } elseif(isset($_POST['deletestickies'])) { // Delete it all! mysql_query("TRUNCATE TABLE stickynews")or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> All <b>stickied</b> news deleted!"; } else { ?> <title>Add/Delete Recent News</title> <a href="../">Click Here to go home.</a> <form action="<? $_SERVER['PHP_SELF'] ?>" method="post"> Title:<br> <input type="text" name="title"></input><br><br> Text:<br> <textarea rows="10" cols="50" name="news"></textarea> <br><br> <input type="submit" name="postnews" value="Post regular News"> <input type="submit" name="poststicky" value="Post sticky news"> </form> </form> <br><br> <h4>Options:</h4> <form action="<? $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="delete" value="Delete all regular news"> <input type="submit" name="deletestickies" value="Delete all stickied news"> </form> <u>Current features:</u> <br> <i>Quick Post News <br> Delete all regular news with one click <br> Delete all stickied news with one click <br> Option to title your news <br> "Sticky" News <br> Neatly displays all posted news in order from sticky to regular</i> <br><br><br> <u>Coming soon:</u> <br> <i>Ability to edit news <br> Ability to make stickied news regular and vise versa</i> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545038 Share on other sites More sharing options...
wildteen88 Posted May 19, 2008 Share Posted May 19, 2008 Did that and I get this: Unknown column 'id' in 'order clause' Do you have a field in your news table which holds an auto_increment key? Substitute id with that field in the following query: $query1 = "SELECT * FROM news ORDER BY id, sticky ASC"; Also, what do I do in my admin part to work with this? Admin part: Recoded admin part: NOTE it is untested. <?php require 'config.php'; // Make a MySQL Connection mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if(isset($_POST['postnews'])) { $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); // check thate status of the sticky radio button $sticky = (isset($_POST['sticky']) && $_POST['sticky'] == 'y') ? 'y' : 'n'; // Insert the news $query = mysql_query("INSERT INTO news (title, news, sticky) VALUES ('$title', '$news', '$sticky')") or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> News posted!"; } elseif(isset($_POST['delete'])) { // delete all non-sticky news if(isset($_POST['delete']['r'])) { $qry = 'DELETE FROM news WHERE sticky=\'n\''; $msg = 'All <b>non-stickied</b> news deleted!'; } // delete all stickied news if(isset($_POST['delete']['s'])) { $qry = 'DELETE FROM news WHERE sticky=\'y\''; $msg = 'All <b>stickied</b> news deleted!'; } // delete all news else { $qry = 'TRUNCATE TABLE news'; $msg = 'All news deleted!'; } // Delete it all! mysql_query($qry)or die(mysql_error()); echo "<meta http-equiv='refresh' content='1'> $msg"; } else { ?> <title>Add/Delete Recent News</title> <a href="../">Click Here to go home.</a> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <p>Title:<br> <input type="text" name="title" /></p> <p>Text:<br> <textarea rows="10" cols="50" name="news"></textarea></p> <p>Sticky: <input type="radio" name="sticky" value="y" /> YES | <input type="radio" name="sticky" value="n" /> NO</p> <input type="submit" name="postnews" value="Post News"> </form> </form> <br><br> <h4>Options:</h4> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="delete" value="Delete All News"> <input type="submit" name="delete[r]" value="Delete Non-Sticky News"> <input type="submit" name="delete[s]" value="Delete Sticky News"> </form> <u>Current features:</u> <br> <i>Quick Post News <br> Delete all regular news with one click <br> Delete all stickied news with one click <br> Option to title your news <br> "Sticky" News <br> Neatly displays all posted news in order from sticky to regular</i> <br><br><br> <u>Coming soon:</u> <br> <i>Ability to edit news <br> Ability to make stickied news regular and vise versa</i> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545063 Share on other sites More sharing options...
Cory94bailly Posted May 19, 2008 Author Share Posted May 19, 2008 Works great... Only thing is that if I make it non-sticky, it will put a space in front of the title. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545072 Share on other sites More sharing options...
Cory94bailly Posted May 19, 2008 Author Share Posted May 19, 2008 Ok so 3 things I found wrong so far. 1.) If non-stickied, theres spaces in front of the title. 2.) For the "Delete all news" button, I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1 3.) For the "Delete sticky news" button, I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545149 Share on other sites More sharing options...
wildteen88 Posted May 19, 2008 Share Posted May 19, 2008 1.) If non-stickied, theres spaces in front of the title. Change the following line in the printNews function: $sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : null; to: $sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : ''; I don't know why you're getting a space for non-sticky news. As both lines above are the same. 2.) For the "Delete all news" button, I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1 3.) For the "Delete sticky news" button, I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1 Argh. Recopy the code from my last post. Had a few SQL errors which I corrected. You must of copied the code before I updated it. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545189 Share on other sites More sharing options...
Cory94bailly Posted May 19, 2008 Author Share Posted May 19, 2008 Same 3 things. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545228 Share on other sites More sharing options...
wildteen88 Posted May 19, 2008 Share Posted May 19, 2008 You sure? I just tested all the queries in my updated code and no errors occurred. Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545236 Share on other sites More sharing options...
Cory94bailly Posted May 20, 2008 Author Share Posted May 20, 2008 You sure? I just tested all the queries in my updated code and no errors occurred. I think I know why. I have that DB set to run mysql version 4.something... Does that query need 5.something? Quote Link to comment https://forums.phpfreaks.com/topic/106106-solved-something-wrong/#findComment-545319 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.