Jump to content

[SOLVED] PHP get method.


neex1233

Recommended Posts

Hi, I am making a news script with a comment system, but I would like it so every time I created a new article, I could post comments like example.com/news.php?artid=1 and I'm pretty sure you use the get method and the array function, but how do you do it? Here is my show news script:

 

<?php
$con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error());
mysql_select_db("DB_Name", $con);

$sql = "
   SELECT title, poster, text, time, date 
   FROM `news` 
   LIMIT 0, 30
"; 
$result = mysql_query($sql) or die (mysql_error());

$results = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
   $results[] = $row;
}

foreach($results as $row) {
   
$text = stripslashes($row['text']);
$title = stripslashes($row['title']);
$text = wordwrap($text, 50, "<br/>\n");
$date = $row['date'];
$time = $row['time'];

   echo "<font size='4'><b>$title</b></font><br>";
   echo "Posted by {$row['poster']} ";
   echo "on $date at $time<br>";
   echo "$text";
   echo "<br> <br>";
}
?>

 

I'd also like to make it so the user can click on the article name and it would shot just that article and it's comments. Could anybody help me? Thanks.

Link to comment
Share on other sites

You'll first have to provide a link to the article.

To do this

 

First change your query so it also returns the article id.

$sql = "

  SELECT id, title, poster, text, time, date

  FROM `news`

  LIMIT 0, 30

";

 

Now you'll want to provide a link for the user to click on to view the article.

$time = $row['time'];

$id = $row['id'];

 

  echo "<font size='4'><b> <a href='article.php?id=".$id."'>$title</a></b></font><br>";

 

Now in article.php the basic code will be

<?php

if(isset($_GET['id'])) && is_numeric($_GET['id']))
{
    $id = (int) $_GET['id'];

    $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id;
    $result = mysql_query($sql);
    if(mysql_num_rows($result) == 1)
    {
        $row = mysql_fetch_assoc($result);
?>
<h1><?php echo $row['title']; ?></h1>
<p>Posted by: <?php echo $row['poster']; ?> On <?php echp $row['date'] . ' ' . $row['time']; ?>
<p><?php echo $row['text'];</p?>
<?php
    }
}
?>

 

 

Link to comment
Share on other sites

its to close the if statements in the first code block. If you notice I closed the first code block before the <h1> tag. As I closed the first code block I didnt close the if statments. Thats why at the end of the page I had to open another PHP code block to close the if statements.

Link to comment
Share on other sites

Okay. Also the final code I am using:

 

<?php $allow = array ('5');include ("/home/username/public_html/folder/protect.php"); ?>
<title>News</title>
<?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']))
{
    $id = (int) $_GET['id'];

    $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id;
    $result = mysql_query($sql);
    if(mysql_num_rows($result) == 1)
    {
        $row = mysql_fetch_assoc($result);
?>
<h1><?php echo $row['title']; ?></h1>
<p>Posted by: <?php echo $row['poster']; ?> On <?php echo $row['date'] . ' ' . $row['time']; ?>
<p><?php echo $row['text'];</p?>
<?php
    }
}
?>

 

Gives me this error:

 

Parse error: syntax error, unexpected T_BOOLEAN_AND in /home/username/public_html/folder/folder/folder/show_news.php on line 7

Link to comment
Share on other sites

Wait, nevermind. I just fixed the code. Here is my final one:

 

<?php $allow = array ('5');include ("/home/username/public_html/folder/protect.php"); ?>
<title>News</title>
<?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']))
{
    $id = (int) $_GET['id'];

    $sql = 'SELECT title, poster, text, time, date FROM `news` WHERE id='.$id;
    $result = mysql_query($sql);
    if(mysql_num_rows($result) == 1)
    {
        $row = mysql_fetch_assoc($result);
?>
<h1><?php echo $row['title']; ?></h1>
<p>Posted by: <?php echo $row['poster']; ?> On <?php echo $row['date'] . ' ' . $row['time']; ?>
<p><?php echo $row['text'];?></p>
<?php
    }
}
?>

 

 

Thanks for your help wildteen!!

Link to comment
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.