neex1233 Posted July 12, 2009 Share Posted July 12, 2009 Hey everyone, I have a PHP comment script (below) and I would like to make it so if the user didn't select an article to view, it shows all of them. It uses the get function to display what they would like, and I would like to make it so if the ?id=1 isn't on the URL, it shows all of the news. Here is the script: <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); if(isset($_GET['id']) && is_numeric($_GET['id'])) if($_GET['id'] == 0) { // Check. echo "Invalid!"; die(); } { $id = (int) $_GET['id']; $sql = 'SELECT * FROM `news` WHERE id='.$id; $result = mysql_query($sql); if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); $text = stripslashes($row['text']); $text = stripslashes($row['text']); $title = stripslashes($row['title']); $title = stripslashes($row['title']); ?> <h2><?php echo "<a href='?id=$id'>"; ?><?php echo $title; ?></a></h2> Posted by <?php echo $row['poster']; ?> on <?php echo $row['date'] . ' at ' . $row['time']; ?> <p><?php echo $text;?></p> <?php } } ?> You can see the 'Check.' comment which is my attempt to do what I wanted to do, which didn't work. Could anybody help me with this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/ Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 if (!empty($_GET['id']) && $_GET['id'] > 0) { $query = sprintf('SELECT * FROM table WHERE id = %u', (int) $_GET['id']); } else { $query = 'SELECT * FROM table'; } Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874044 Share on other sites More sharing options...
neex1233 Posted July 12, 2009 Author Share Posted July 12, 2009 So where do I put that? Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874046 Share on other sites More sharing options...
neex1233 Posted July 12, 2009 Author Share Posted July 12, 2009 Actually, that script doesn't work. I tried it. Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874058 Share on other sites More sharing options...
manny Posted July 12, 2009 Share Posted July 12, 2009 hold on Help is on the way give me like 5 minutes Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874100 Share on other sites More sharing options...
manny Posted July 12, 2009 Share Posted July 12, 2009 Try to go something like this <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); $id = !empty($_GET['id'])?(int)$_GET['id']:0; if(!empty($_GET['id']) && $_GET['id'] != 0) { // Check. //echo "Invalid!";die(); $sql = 'SELECT * FROM `news` WHERE id="'.$id.'" ORDER BY `date`'; $post_num = 'single'; } else { $sql = 'SELECT * FROM `news` ORDER BY `date`'; $post_num = 'multi'; } $result = mysql_query($sql); if ($post_num == 'multi') { $x = 0; while ($row = mysql_fetch_assoc($result)) { $listings[$x]['text'] = htmlspecialchars_decode($row['text']); $listings[$x]['title'] = htmlspecialchars_decode($row['title']); $listings[$x]['date'] = $row['date']; /// Keep adding lines for every field you want to include /// Keep adding lines for every field you want to include /// Keep adding lines for every field you want to include $x++; } } elseif ($post_num = 'single') { $row = mysql_fetch_assoc($result); $listings['text'] = htmlspecialchars_decode($row['text']); $listings['title'] = htmlspecialchars_decode($row['title']); $listings['date'] = $row['date']; /// Keep adding lines for every field you want to include /// Keep adding lines for every field you want to include /// Keep adding lines for every field you want to include } switch ($post_num) { case 'single': ?> <h2> <?php echo '<a href=?id='.$id.'">"'. $title; ?></a> </h2> Posted by <?php echo $row['poster']; ?> on <?php echo $row['date'] . ' at ' . $row['time']; ?> <p><?php echo $text;?></p> <? break; case 'multi': foreach ($listings as $listing) { echo '<a href=?id='.$id.'">"'. $title.'</a></h2>'; } break; default: //if none exist you have an error in your code break; } Let me know how it goes Quote Link to comment https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874116 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.