ferret219 Posted June 17, 2007 Share Posted June 17, 2007 Hello, I have just started in PHP to create a news system for the site I am developing. The idea is to display 3 of the latest news items on the home page, which I have done. But to save space, I have tried to create it so that only the titles are shown and when the user clicks on the title, the body of the story is shown. I have found the relevant code for this and put it into my page but the whole thing seams not to be working. All three of the title links open the top news story, but that is the smallest problem as the body of the news story is not being displayed, although the title is. I am also getting the following message at the bottom of the page: PHP Notice: Undefined property: stdClass::$news_body in C:\Inetpub\wwwroot\index.php on line 140 Can anyone help please? <head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style type="text/css"> <!-- .style1 { font-family: Arial, Helvetica, sans-serif; font-size: 36px; } .style2 {font-family: Arial, Helvetica, sans-serif; font-size: 24px; } .style3 { font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 18px; } a { font-family: Arial, Helvetica, sans-serif; } --> </style> <style type="text/css"> <!-- .container { font-family:verdana,arial,helvetica,sans-serif; font-size:16px; margin:4px 0; } .container img { vertical-align:bottom; } .container span { font-weight:bold; cursor:pointer; } .off { display:none; } .on { display:block; margin:10px 30px; text-align:justify; color:#003; background-color:#eef; } --> </style> <script type="text/javascript"> <!-- window.onload=function() { spn=document.getElementsByTagName('span'); for(c=0;c<spn.length;c++) { spn[c].onclick=function() { if(this.id!='') { stuff(this.id.split('s')[1]); } } } } function stuff(n) { dvs=document.getElementsByTagName('div'); ims=document.getElementsByTagName('img'); info=document.getElementById('div'+n); pic=document.getElementById('img'+n); for(c=0;c<dvs.length;c++) { if((dvs[c].className=='on')&&(dvs[c].id!='div'+n)) { dvs[c].className='off'; } } for(c=0;c<ims.length;c++) { ims[c].src='images/plus.GIF'; } if(info.className=='on') { info.className='off'; pic.src='images/plus.GIF'; } else { info.className='on'; pic.src='images/minus.GIF'; } } //--> </script> </head> <body> <p align="center"> </p> <table width="80%" border="0" align="center"> <tr> <td height="20"> </td> </tr> <tr> <td><p class="style2">Latest News:<br></p> <?php //includes include('conf.php'); // Create database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); // Select database mysql_select_db($db) or die ('Unable to select database'); // Generate query and execute $query = "SELECT news_id, news_title FROM news ORDER BY news_id DESC LIMIT 0,3"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // If records present if (mysql_num_rows($result) > 0) { // Itterate through result set // Print news items while($row = mysql_fetch_object($result)) { ?> <div class="container"> <span id="s0"><?php echo $row->news_title; ?></span> <div id="div0" class="off"> <?php echo $row->news_body; ?> </div> <?php } } // If no records present // Display message else { ?> <font size="-1">No records currently avaliable</font> <?php } // Close database connection mysql_close($connection); ?> </td> </tr> <tr> <td> </td> </tr> </table> </body> </html> Thanks in advance, - Chris - Quote Link to comment https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/ Share on other sites More sharing options...
sasa Posted June 17, 2007 Share Posted June 17, 2007 in your query you don't select news_body just news_id and news_title $query = "SELECT news_id, news_title, news_body FROM news ORDER BY news_id DESC LIMIT 0,3"; Quote Link to comment https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/#findComment-276235 Share on other sites More sharing options...
ferret219 Posted June 17, 2007 Author Share Posted June 17, 2007 lol. It's amazing what you can miss even after spending an hour trawling through the code. Thank you very much for the help. However, I still have the problem of the three links opening just the top story, under the top story heading. I think that this may be more of a javascript problem but if anyone can help I would be very greatful. P.S: I have uploaded the file to: http://186.awardspace.com/index.php Quote Link to comment https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/#findComment-276263 Share on other sites More sharing options...
GingerRobot Posted June 17, 2007 Share Posted June 17, 2007 Just took a quick look at the source on the page you uploaded and found: <span id="s0">This is the latest post</span> <div id="div0" class="off"> Hello world</div> <div class="container"> <span id="s0">Title HERE</span> <div id="div0" class="off"> this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post.</div> <div class="container"> <span id="s0">fred</span> <div id="div0" class="off"> bloggs</div> All of the IDs are div0 or s0. Im assuming those numbers where supposed to be incremented. In which case, try: <?php $x = 0; if (mysql_num_rows($result) > 0) { // Itterate through result set // Print news items while($row = mysql_fetch_object($result)) { ?> <div class="container"> <span id="s<?php echo $x; ?>"><?php echo $row->news_title; ?></span> <div id="div<?php echo $x; ?>" class="off"> <?php echo $row->news_body; ?> </div> <?php $x++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/#findComment-276269 Share on other sites More sharing options...
ferret219 Posted June 17, 2007 Author Share Posted June 17, 2007 Once again, thank you for the quick reply. I have set the all of the identifier tags to the news_id using the php echo() command and the site now works perfectly. Thanks for all your help, - Chris - Quote Link to comment https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/#findComment-276272 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.