Jump to content

Recommended Posts

generally, it is used to identify a particular record in a database. ie you have a html table of pictures. As part of that table each picture has a link with an 'id' associated with it. when you click the link it takes you to a page where the $_GET identifies which record in the database to get the information regarding the picture. This is a simple example.

$_GET[] is a built in array.

 

You generally set the $_GET[] in one of two ways. Either through a web form with method="GET" or via a query string.

 

So if you had a script and want to pass in some data via a query string you might do:

 

Yourfile.php?id=32;

 

The query string is everything after the ? That gets placed in the $_GET[] array by default.

 

You can then access that array by using:

 

 


$variable = $_GET['id'];

 

You could of course have multiple parts to the query string like Yourfile.php?id=32&de=43

 

So you could access multiple variables from the GET array.

 

 

Query strings are very handy for passing in parameters for like the ID of a page or a piece of content. Look at the url of this site and you'll see things like topic=350294.0.

 

So your script could then do

 

$tipic = $_GET[topic];

So the variable $topic is now set as 350294.0 and as your script now has access to this you can use it however you like - most likely in a query on the database to pull that particular article.

 

You would most likely use the query strings and $_GET[] to identify the topic you want to pull out from a database (in this instance).

 

So depending how you separate your code you would use the $_GET[] on whatever page processes the DB query and displays the content.

 

 

So on one page you might generate a list of anchor tags like

 


<a href="/SelectedTopics.php?id=20"> Go to topic 20 </a>

 

When a user clicks that linkt they'll go to the Select.php and the in the address bar there will be the querystring on the end of ?id=20

 

So your script would then do something like :

 

$id = $_GET['id'];  // Here you assign the query string that is stored in the $_GET array to a new variable name - $id

 

Then you run a database query using that ID  and generate your content.

pls just take the pain to help me fix where it should be in this two scripts

<?php
include"header.php";
if(isset($_POST['submit']))
{
$topic=$_POST['topic'];
$cat=$_POST['cat'];
$content=$_POST['content'];
$dateTime = new DateTime("now", new DateTimeZone('GMT'));
$date =$dateTime->format("Y-m-d H:i:s");
$ins="INSERT INTO topics(topics_subject,topics_content,topics_cat,topics_date)VALUES('$topic','$content','$cat','$date')"; 
$inss=mysql_query($ins) or die(mysql_error());
if($inss)
{
echo "<a href='topic_dis.php'>Your topic as been inserted</a>";
$topicid=mysql_insert_id();
$insert= "INSERT INTO post(post_content,post_date,post_by)VALUES('$content','$date','$topicid')";
mysql_query($insert) or die(mysql_error());
}
}
?>

 

this is the page that displays the topics

<?php
                                 include"header.php";

                                  $sql="SELECT *FROM topics ";
                            $result=mysql_query($sql)or die(mysql_error());
                                while($row=mysql_fetch_array($result))
                             {

                               echo"<a href='comment.php'>{$row['topics_subject']}</a>"."</br>";
                             }

                                         ?>

Hi mate

 

 

I am assuming your queries actually work. If so, here's the script to generate the links with the IDs. Typing this off the top of my head so sorry if there are any errors.

 


$result = mysql_query("SELECT * FROM topics");

while($row = mysql_fetch_array($result))
  {

  
  echo '<a href="/YourScript.php/?id=';
  echo $row['id'];
  echo '">' . $row['topic_subject'] . ' </a>';
  echo "<br/>";
  
  }

 

You need to change YourScript.php to whatever script the user will be directed to in oder to generate the content. But this should generate a list of topics with the links dynamically created from the IDs in the database.

@kleb:  why you are duplicating your content in the tables topics and post?  ... no make any sense... seems to me that you have a big confusion between the answers to your previous post where Drongo was helping you, and the ones here.

 

Just to refresh the goals that you explain in your original post

Ok. This is what i mean: i want users to post Essays on my site just like a forum. what i did was i stored the topics of those Essays in a table i named TOPICS and the body of the Essay to another table i called CONTENT, i have selected all the topics and echoed it out on a page so that intrested users can click on them and read the whole Essay but my problem is how i can link those TOPICS to their specific CONTENTS. THanks

Yeah, looking at the two INSERT queries above I see:

$ins="INSERT INTO topics(topics_subject,topics_content,topics_cat,topics_date)VALUES('$topic','$content','$cat','$date')"; 

$insert= "INSERT INTO post(post_content,post_date,post_by)VALUES('$content','$date','$topicid')";

 

The second insert statements has absolutely no value as it is only storing data that already exists in the 'topics' table. You should just drop that table completely. Also, you do not need to create a date variable. Just create a datetime field in the database that is automatically populated with a timestamp when the record is created. Also, it is not clear from your code what type of value category is. It *should* be a numeric integer which is a foreign key back to a category table.

 

Anyway, I decided to rework all of the logic from  the beginning.

 

Here is the table I used that has the category as an integer and data as a field that is automatically set to the current timestamp

CREATE TABLE `topics` (
`topics_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`topics_subject` VARCHAR( 256 ) NOT NULL ,
`topics_content` VARCHAR( 5000 ) NOT NULL ,
`topics_cat` INT NOT NULL ,
`topics_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = MYISAM ;

 

Here is the script to insert records

include"header.php";
if(isset($_POST['submit']))
{
    $topic = mysql_real_escape_string(trim($_POST['topic']));
    $cat_id = intval($_POST['cat']);
    $content = mysql_real_escape_string(trim($_POST['content']));
    $query = "INSERT INTO topics
                  (topics_subject, topics_content, topics_cat)
              VALUES
                  ('$topic', '$content', '$cat_id')"; 
    $result = mysql_query($query) or die(mysql_error());
    if($result)
    {
        echo "<a href='topic_dis.php'>Your topic as been inserted</a>";
    }
    else
    {
        echo "There was a problem inserting your record.";
    }
}

 

Here is the script to display a list of records (again, this assumes a separate table of categories with fields for the category id and category name)

    $query = "SELECT topics_id, topics_subject, topics_date, category
              FROM topics
              JOIN categories ON topics.topics_cat = categories.category_id
              ORDER BY topics_date DESC";
    $result = mysql_query($query) or die(mysql_error());
    if(!$result)
    {
        echo "There was a problem getting the topics.";
    }
    else
    {
        echo "<table border='1'>\n";
        echo "  <tr>\n";
        echo "    <th>Subject</th>\n";
        echo "    <th>Category</th>\n";
        echo "    <th>Date</th>\n";
        echo "  </tr>\n";
        while($row = mysql_fetch_assoc($result))
        {
            $displayDate = date('m-d-Y', strtotime($row['topics_date']));
            echo "  <tr>\n";
            echo "    <td><a href='comment.php?id={$row['topics_id']}'>{$row['topics_subject']}</a></td>\n";
            echo "    <td>{$row['category']}</td>\n";
            echo "    <td>{$displayDate}</td>\n";
            echo "  </tr>\n";
        }

        echo "</table>\n";
    }

 

Now, on the page 'comment.php' you would use $_GET['id'] to get the id of the topic that the user clicked. You would use that id to query for the appropriate data of that record to display on the page.

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.