Get the Item id when creating dynamic pages on wordpress - php

I've been searching for several days now trying to find how to retrieve the item id of a select item from a mySQL database on a wordpress website.
I have a database of several hundreds of recipes. They are all displayed in a list on a certain page. I then want to be able to click on one of the recipes in the link and be sent to a unique recipe.php page.
The list of all recipes is on main-page.php. On this page, I have created the following:
echo '<td class="recipe"><a href=recipe?id='.$id.'><div class="recipe-container"><img src="'.$img.'" id="recipe-image"/>';
echo '<div class=recipe_name>'.$nam.'</a></div>';
As you can see, clicking on a recipe with the id of 4 would send you to www.example.com/recipe?id=4.
My question is, how do I tell the code to recognize that we are on the page for recipe #4, so that I can display its unique details? In particular, what do I write in recipe.php so that I can display the unique content for the selected recipe?

create an onload() event on the recipe.php, then check for $_SERVER['QUERY_STRING'] to retrieve id value and retrieve data from database based on the value

Found the awnser. All I had to do was:
$id = $_GET['id']
Had no idea it would be that easy.

Related

How to get search key when someone edit the result after search

In my project there is list of some records. User can search the records by project name. After search user can edit particular record. After edit when user click on Update button right now it is redirecting to list page but the search result gone and all records are displaying.
After Update i want to display the searched records only not all. How to get search key back ? I dont want to use session for that if any other better way.
I am using codeigniter.
For example:
There is a list of 10 records. User search the records by "ABC". Result come which has "ABC" project like 5 records come. Then user edit one record and update it. After update I wan to display that 5 records with search key "ABC" only. Right now it is displaying 10( ALL) records because i am redirecting on list page without search key. I have no idea how to do it. :(
What i would do personally is something like this on my list Page i would have a form which would be submitted to search function through GET method. This way i would have all my search Params in the URL and my URL would be something like this :
exampledomain.com/users?name=steve&age=23
Now for the edit page i would generate a link with a special parameter say : lastpage whose value would be some encoded form of the current search params. So when the user would go to the edit page the URL would be somthing like this:
exampledomain.com/users/edit/34?lastpage=erasc1235234safvdadsrgdfgbvcdfgrt435
when the edit functunality is completed. I would pick this lastpage Param form $_GET['lastpage'] ( or in CI you can use $this->input->get('lastpage')) to get the encoded value of lastpage and decode that to get all the previous URL search Params. and redirect the user to the serach function again with the search params attached to the URL.
I solved the same problem i had with dividing the view to two types
1) the normal view which shows all records and
2) the other view which shows all recently searched records for the last searched key.
To achieve this u need to store the searched records' ids and place from which the records have been taken to be stored in a temporary DB Table like TableforSearch and store the search key like 'ABC' in session.
On search of the Key 'ABC' find the ids of the records which matches the Key and save it to Table TableforSearch along with the Search Key and UserId.
Then use the Session to get the Key and search the table with the Key in Session to fetch the row that has All the Ids of the Searched Key.
For Error Handling see if the Ids present in Table have not been deleted for that Record.
And finally on Showing of searched items Show the second Page , for the other show normal first page
In order to get the keyword that has been type. You have three options
Save keyword as GET variable
Save keyword as hidden input
Save keyword as SESSION variable
Save keyword as COOKIEvariable
Since that you don't to choose the option 3 and 4. Your only option is to create a GET variable.Just add domain.com/controller/function?keyword=ABCand get that on controller as rldecode($_GET['keyword']); or create hidden input where you can save you value and fetch that value.
One way around is that, when user searches anything in list page (I hope your list page and search page are the same) than every time on new search, store the search keywords in localStorage and when user updates the record than redirect to list page along with your search keywords from localStorage. (This is suggested only if your list page and search page are the same. i.e. you handle the search with some parameters passed to list page.)
save your search criteria in a session variable $_SESSION['mysearch'] = 'ABC' and use it when redirecting header("Location: /mysearch.php?criteria=" . urlencode($_SESSION['mysearch'])).
Then in your "mysearch.php" you would ureldecode($_GET['criteria']), run the search again and present the results.

1 php page, different contents based on an identifier

I want to make a PHP page that will changing it's content based on some sort of an ID.
The idea is: the index page will have 4 squares, the content of those 4 squares will be the top 4 records in the database, they obviously have their own IDs in the DB.
What I want to happen is when I click on one of the squares it will pass the ID to another PHP page that will get all the details about it in the page.
To be more clear:
Lets say it is a cars website, the 4 squares would be an image of top 4 cars in the database with the IDs 1,2,3 and 4 respectively, when I click on the car's image (lets say 1) I will be directed to a PHP page called CarInfo.php
what i want to happen here is for the ID of the square to be passed to this page (maybe page will appear something like carInfo.php?id=1) and the page will load all the information from the database where the ID will match the recieved ID (in this case the record with ID = 1).
The problem is I don't even know how to start doing it... How can I pass the ID? How can the other page receive it? And can I use a variable to pass it to so I can use it in the query carteria? If so how?
Note: there will be 1 PHP page that displays the information ONLY (aside from index), all content will be dynamic.
very easy. exactly as you have said. you will use address with parameter, like
carInfo.php?id=1
and on carinfo page you can use php get variable http://php.net/manual/en/reserved.variables.get.php
$_GET["id"]
it will give you selected id, and you can then use is in database
In the first page you have to do something like this for each car:
Click Here
Click Here
Click Here
Click Here
Then, in show.php you need to write a code like this to recive information form the provided ID if the database you're using is MySQL:
<?php
$db = mysqli_connect('localhost','USERNAME', 'PASSWORD','DATABASE');
$result = mysqli_query($db, "SELECT * FROM YOUR-TABLE WHERE id = '{$_GET["id"]}'");
while($row = mysqli_fetch_assoc($result)){
echo "Color : {$row['color']}";
// ...
}
?>

MYSQL table suggestions

I am new to MySQL and PHP. I am having issues wrapping my mind around how to accomplish something. I am building a site that has basically a forum style post page. Users enter text into a textarea which then posts that text along with a timestamp and $_SESSION['Username'] into a MySQL table titled "campaigns." So the table has postEntry, postName and postDate rows currently.
On this same page that I have the form, I then display the entire contents of the campaigns table into a div. So it can show each post in descending order.
This has been working great for me, but I am now trying to look at the bigger picture and am thinking this is not a good way to do what I need. I basically need the ability to have an endless amount of "campaigns" each with their own set of posts. Then give the user the ability to select which campaign they want to view and show corresponding posts for that campaign in the div.
So the real question is: Is there a way to do this with just one table. Or would each campaign need it's own table in the database?
Add a campaign_id to the POST table and viola!
edit: more info:
you need one table for the campaign like so:
Campaign
-------------
campaign_id
name
then you need another one for all the posts
post
-------------
post_id
campaign_id
post_time
name
this way, each post is associated to a specific named campaign.

PHP edit unique row in table

I currently have a PHP form that uses AJAX to connect to MySQL and display records matching a user's selection (AJAX: Display MySQL data with value from multiple select boxes)
As well as displaying the data, I also place an 'Edit' button next to each result which displays a form where the data can be edited. My problem is editing unique records since currently I only use the selected values for 'name' and 'age' to find the record. If two (or more) records share the same name and age, I am only able to edit the first result.
Let's assume your file for editing is edit.php. Then, in the file where you generate the edit links, try changing your edit button link as follows:
'edit'
Then you will be able to access ID variable as
echo $_REQUEST['ID'];
Note that the ID is case sensitive. Let me know how it goes.
when displaying records from ajax, also send the primary field(id in most cases) along with name and age
and when u are displaying these data along with edit incorporate that primary field with edit

php search results

I've seen alot of tutorials on search with php and mysql, but im having trouble with generating a link with the search result. For example say i have an item called "item1" in my db and the user searhes for item , item1 should be returned as a link so the user can click the link to get more information about that product. Does anyone have any scripts , or snippets of code for how to acheive this?Thanks
You probably need to create an item page say item.php that accepts an id which will then search teh database for that item and display the item information.
Your search results will then have to display the name of the item in a link that also includes the id of the item.
<?php echo $itemname"?>
This would of course be in a loop that goes through the list of items one at a time.
When the user clicks the link it will take them to item.php and send id as a parameter.
that's a simply example with no check and no control by datatype or query results. just start from it and do what you need...
<?php
//your db connection
//col1 is where the id is saved
//col2 is the url
$qry="SELECT col1, col2 FROM table WHERE col1='".$_GET['var']."'";
$result=mysql_fetch_array(mysql_query($sql));
echo 'LINK';
?>
edit:
if you want a direct redirect do this instead of echoing
header('Location: '.$result['col2']);

Categories