kleb Posted December 20, 2011 Share Posted December 20, 2011 Please i will like to know what this variable $_GET['id']is used for and how to use it Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/ Share on other sites More sharing options...
litebearer Posted December 20, 2011 Share Posted December 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299719 Share on other sites More sharing options...
Drongo_III Posted December 20, 2011 Share Posted December 20, 2011 $_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. Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299723 Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 now am getting it ... but on what page will the variable be.. i mean will it be on the insert.php page where i treat the posted topics or on the select.php page where i display selected topics Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299740 Share on other sites More sharing options...
Drongo_III Posted December 20, 2011 Share Posted December 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299745 Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299757 Share on other sites More sharing options...
Drongo_III Posted December 20, 2011 Share Posted December 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299763 Share on other sites More sharing options...
mikosiko Posted December 20, 2011 Share Posted December 20, 2011 @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 Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299772 Share on other sites More sharing options...
Psycho Posted December 20, 2011 Share Posted December 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299786 Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 Thanks so much am going to try it very soon bcos am busy now..thanks again Quote Link to comment https://forums.phpfreaks.com/topic/253547-what-is-_getid-used-for/#findComment-1299789 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.