aled2305 Posted June 6, 2013 Share Posted June 6, 2013 Hello, I now have a working blog, but only want some blog posts to show. I have a table called 'post_type' and have got the numbers 1 and 2 entered.Here is the code of my blog. Should this only show the posts with 1 in the table post_type ? If not, can you please tell me what's wrong... <?php require_once('Connections/check_mag.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_check_mag, $check_mag); $query_getRecent = "SELECT news.post_id, news.title, news.post_type FROM news WHERE news.post_type = 1 ORDER BY news.updated DESC "; $getRecent = mysql_query($query_getRecent, $check_mag) or die(mysql_error()); $row_getRecent = mysql_fetch_assoc($getRecent); $totalRows_getRecent = mysql_num_rows($getRecent); $var1_getDisplay2 = "-1"; if (isset($_GET['archive'])) { $var1_getDisplay2 = $_GET['archive']; $query_getDisplay = sprintf("SELECT news.post_id,news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text")); } elseif (isset($_GET['post_id'])) { $var2_getDisplay3 = $_GET['post_id']; $query_getDisplay = sprintf("SELECT news.post_id,news.title, news.image, news.author, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int")); } else { $query_getDisplay = "SELECT news.post_id,news.title, news.image, news.author, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC"; } $getDisplay = mysql_query($query_getDisplay, $check_mag) or die(mysql_error()); $row_getDisplay = mysql_fetch_assoc($getDisplay); $totalRows_getDisplay = mysql_num_rows($getDisplay); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Blog</title> <link href="blog.css" rel="stylesheet" type="text/css" /> <style type="text/css"> a:link { color: #82BA63; text-decoration: none; } a:visited { text-decoration: none; color: #82BA63; } a:hover { text-decoration: none; color: #6FAE4D; } a:active { text-decoration: none; color: #82BA63; } </style> <script type="text/javascript"> function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } </script> </head> <body onload="MM_preloadImages('images/techyoucation grey.png')"> <div id="blog"> <div id="header"><a href="http://techyoucation.com/blog" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Logo','','images/techyoucation grey.png',1)"><img src="images/full logo.png" alt="techyoucation logo" width="219" height="47" id="Logo" /></a></div> <?php do { ?> <div class="blog_post"> <h4><img src="images/clock.png" width="22" height="22" alt="clock" /> <?php echo $row_getDisplay['formatted']; ?> </h4> <img src="<?php echo $row_getDisplay['image']; ?>" height="200" alt="icon" /> <div id="blog_text"><span style="color:#A0A0A0;font-family:Arial;font-size:25px;"><a href="<?php echo $row_getDisplay['post_id']; ?>"><?php echo $row_getDisplay['title']; ?></a></span><br /> <span style="color:#D0D0D0;font-family:Arial;font-size:15px;">By <?php echo $row_getDisplay['author']; ?></span><br /> <br /> <?php echo substr ($row_getDisplay['blog_entry'],0 ,500); ?>...<br /> <a href="<?php echo $row_getDisplay['post_id']; ?>">Read More</a><br /> <br /> </div> </div> <?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?><span style="color:#82BA61;font-family:Arial;font-size:17px;"> <strong>© Copyright 2013 techyoucation.com</strong> </span> </div> </body> </html> <?php mysql_free_result($getRecent); mysql_free_result($getDisplay); ?> Thanks in advance Aled Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 Assuming $getRecent is the resource you're referring to, yes it should only get a post_type of 1 - WHERE news.post_type = 1. That said, I can't see you using taht resource anywhere other than the two lines after it. Personally, I can't be bothered looking through it again because You've not actually asked specific question or described a specific problem. You've given us a code dump of your entire page; frankly we don't care what DTD you're using among other things included. None of your code has comments making it really hard to understand. You've not provided us with any debugging steps you have carried out yourself. Rewrite your post with details of the specific problem, specific code snippets related to the problem, comments on what the code is doing and anything you have done off your own back to find the problem. I appreciate its your second post here, but how do you expect us to help you with a problem if you can't be bothered to help us identify the problem? Quote Link to comment Share on other sites More sharing options...
aled2305 Posted June 6, 2013 Author Share Posted June 6, 2013 Okay @cpd I will try and make things easier, but would like to let you know I have very little php knowledge. Most of the code has been down using parts of tutorials and Dreamweaver, therefor as stupid as it sound, I don't actually know what half of the code does my self. The actual problem is that it still shows every post, and not only the ones with 1 in the post_type table. Thanks Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 (edited) You can't come here every time you hit a small error asking what's wrong. You'll never get anywhere mate! Lets abstract the problem so you can learn something. Create a new PHP page and set up a connection to your database. Now take this code and add it to your new PHP file $query_getRecent = "SELECT news.post_id, news.title, news.post_type FROM news WHERE news.post_type = 1 ORDER BY news.updated DESC "; $getRecent = mysql_query($query_getRecent, $check_mag) or die(mysql_error()); And see whats happening by adding after the query code while($row = mysql_fetch_assoc($getRecent) { var_dump($row); } What are the results? Edited June 6, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
aled2305 Posted June 6, 2013 Author Share Posted June 6, 2013 Okay so I have <?php require_once('Connections/check_mag.php'); ?> <?php mysql_select_db($database_check_mag, $check_mag); $query_getRecent = "SELECT news.post_id, news.title, news.post_type FROM news WHERE news.post_type = 1 ORDER BY news.updated DESC "; $getRecent = mysql_query($query_getRecent, $check_mag) or die(mysql_error()); while($row = mysql_fetch_assoc($getRecent) { var_dump($row); } ?> and the result it http://techyoucation.com/blog/test.php Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 Nice work setting the test up. You've obviously copied & pasted my code as the typo was there. The error tells us there's a syntax error on line 6, so we should review line 6 to see if anything is wrong. Typical syntax mistakes include 1) forgetting a semi colon; 2) not closing brackets - all variations; 3) misspelling variable/function names. One of the above is your problem. Quote Link to comment Share on other sites More sharing options...
aled2305 Posted June 6, 2013 Author Share Posted June 6, 2013 Okay fixed it, I think Added extra ')' results are on same page Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 (edited) Well done. Do yourself a favor and make it easier to read by putting echo '<pre>'; above the while loop. Now verify the output is consistent with your database entries, i.e. its only retrieving posts with a type of 1. If so you know your query is correct. Edited June 6, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
aled2305 Posted June 6, 2013 Author Share Posted June 6, 2013 Yes the results match the database and are only showing the ones with 1 in the table. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 (edited) Brilliant. So you've now verified through a series of debugging steps the data you're retrieving is correct therefore, the query is correct and doesn't need adjusting. You did that yourself with a bit of guidance - not that hard is it? Where else do you think the problem could be? Are you even attempting to display these results within your page? Edited June 6, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
aled2305 Posted June 6, 2013 Author Share Posted June 6, 2013 (edited) Okay, that's great. Well on line 113 for example it says getDisplay but we just edited getRecent, could this be anything to do with it? <?php require_once('Connections/check_mag.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_check_mag, $check_mag); $query_getRecent = "SELECT news.post_id, news.title, news.post_type FROM news WHERE news.post_type = 1 ORDER BY news.updated DESC "; $getRecent = mysql_query($query_getRecent, $check_mag) or die(mysql_error()); $row_getRecent = mysql_fetch_assoc($getRecent); $totalRows_getRecent = mysql_num_rows($getRecent); $var1_getDisplay2 = "-1"; if (isset($_GET['archive'])) { $var1_getDisplay2 = $_GET['archive']; $query_getDisplay = sprintf("SELECT news.post_id,news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text")); } elseif (isset($_GET['post_id'])) { $var2_getDisplay3 = $_GET['post_id']; $query_getDisplay = sprintf("SELECT news.post_id,news.title, news.image, news.author, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int")); } else { $query_getDisplay = "SELECT news.post_id,news.title, news.image, news.author, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC"; } $getDisplay = mysql_query($query_getDisplay, $check_mag) or die(mysql_error()); $row_getDisplay = mysql_fetch_assoc($getDisplay); $totalRows_getDisplay = mysql_num_rows($getDisplay); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Blog</title> <link href="blog.css" rel="stylesheet" type="text/css" /> <style type="text/css"> a:link { color: #82BA63; text-decoration: none; } a:visited { text-decoration: none; color: #82BA63; } a:hover { text-decoration: none; color: #6FAE4D; } a:active { text-decoration: none; color: #82BA63; } </style> <script type="text/javascript"> function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } </script> </head> <body onload="MM_preloadImages('images/techyoucation grey.png')"> <div id="blog"> <div id="header"><a href="http://techyoucation.com/blog" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Logo','','images/techyoucation grey.png',1)"><img src="images/full logo.png" alt="techyoucation logo" width="219" height="47" id="Logo" /></a></div> <?php do { ?> <div class="blog_post"> <h4><img src="images/clock.png" width="22" height="22" alt="clock" /> <?php echo $row_getDisplay['formatted']; ?> </h4> <img src="<?php echo $row_getDisplay['image']; ?>" height="200" alt="icon" /> <div id="blog_text"><span style="color:#A0A0A0;font-family:Arial;font-size:25px;"><a href="<?php echo $row_getDisplay['post_id']; ?>"><?php echo $row_getDisplay['title']; ?></a></span><br /> <span style="color:#D0D0D0;font-family:Arial;font-size:15px;">By <?php echo $row_getDisplay['author']; ?></span><br /> <br /> <?php echo substr ($row_getDisplay['blog_entry'],0 ,500); ?>...<br /> <a href="<?php echo $row_getDisplay['post_id']; ?>">Read More</a><br /> <br /> </div> </div> <?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?><span style="color:#82BA61;font-family:Arial;font-size:17px;"> <strong>© Copyright 2013 techyoucation.com</strong> </span> </div> </body> </html> <?php mysql_free_result($getRecent); mysql_free_result($getDisplay); ?> Edited June 6, 2013 by aled2305 Quote Link to comment Share on other sites More sharing options...
cpd Posted June 6, 2013 Share Posted June 6, 2013 (edited) Well you want to display the results of the query we just verified yes? If so, you need to cycle through the result set, like you did in your debugging, and print out the HTML with the values you've retrieved. E.g. <?php while($row = mysql_fetch_assoc($getRecent)) : ?> <tr> <td><?=$row['someValue'];?></td> <td><?=$row['anotherValue'];?></td> <td><?=$row['fooBar'];?></td> </tr> <?php endwhile; ?> I can't see you trying to loop through the results anywhere in your script. Edited June 6, 2013 by cpd 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.