Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/165694-php-getif/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/165694-php-getif/#findComment-874116
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.