Dynamic page creation PHP + MySQL - php

Been struggling with this problem for a while now and I just can't het my brain to understand it.
I have a very simple website where I can add items in a database. There is a list on index.php where the list is displayed and each item needs a url that directs to a "more information" page.
The "more information" page has to be a dynamic one as there are a lot of items and these items can be added or deletend.
What my code for this section looks like at the moment:
$result_set = mysql_query("SELECT id, name FROM items WHERE id = $item");
while ($item = mysql_fetch_row($result_set)) {
$name = $item['name'];
echo "$name";
This results in a link if item 1 = wrench ../items/wrench.html.
But this page obviously doesn't excist. How can I get this to work?

You can't have one html page for each item if the items are dynamically added
But you can do it this way
echo '$name';
This way you have only one page who receive as a GET parameter the id of the item you want the description.
In the page of more_information.php you just display a text corresponding to the id you received.

use .htaccess for this.
you can add this code
RewriteEngine On
RewriteRule ^items/(.+)$ items.php?page=$1
In your items.php use $items=$_GET[page]; so you can read what is in url after items/
This is a link where you can find more about RewriteRule's http://httpd.apache.org/docs/current/rewrite/intro.html

Related

Using PHP hyperlinks to extract data from mysql table

I am currently stuck on how to use $_GET from a hyperlink and send it to another php page for processing.
I have 2 php pages. The first page, we'll call it page1.php, gets user input (customerID) from a HTML page through a textbox and according to that id, goes into the database and finds relative information such as the Customer Name and the Address etc using logical joins.
$id = $_GET['custID']; //This is to obtain the input from the HTML page.
I have that working perfectly.
My next task is to create another php page, we'll call it page2.php, in where i am required to output only the customerID and the customerName, and from there i am required to hyperlink every record in the Customer ID row which references to page1.php and when the user clicks any of those links, it should only show information according to that customerID in a table.
So far i can make each record in the Customer ID row link to page1.php but i am unable to output any results.
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["customerName"]?></td>
<td><?php echo "<a href='page1.php?'>{$row["custID"]}</a>"?></td>
</tr>
<?php }
Any help would be much appreciated.
Change the link to following
<?php echo ''.$row["custID"].'';?>

How do I add GET data into an anchor tag so when users click it the data shows up in the URL?

I have a anchor tag and I would like it to pass GET data into the URL so I can access that data from a php script.
Here is an example
<a href="template.php" GET="product1"/>
<a href="template.php" GET="product2"/>
The above code does not work so I imagine that is not how it is done.
What I am trying to do is pass the GET data to template.php so it can display things depending on what the user clicks and what get data is sent.
Just create a querystring with a key that has meaning to you (I chose "item") and assign it the product key as the value:
Product 1
Now when a user clicks on that link you will have a $_GET variable of product1:
$item = $_GET['item']; // product1
Like this:
product1

Print multiple PHP Files whitout opening them

I have a PHP system that have orders, and items related to orders,
So I want to print every item related to specific order, without open the file.
I'm using a SQL query to show the pages:
SELECT * FROM items WHERE order_id = ".$oid.";
to collect items from this order and for each one, I write
<a href="view.php?iid=<?php echo $theResult['id'];?>" target="_blank">
Item <?php echo $theResult;?>
</a>
So the page view.php is a default that receive item_id in $_GET and build the page as I want.
There's no problem until here, BUT:
I want to print any result for view.php, for each item in order, without have to open and click to print everyone.
If you insist on still using the single view file view.php, a bit of a hacky solution could look like this:
<?php
foreach ($items as $iid) {
$_GET["iid"] = $iid;
require("view.php"); //This will print whatever the view.php page does
}
?>
Here $items specifies an array full of the ID's from the database.
By setting $_GET["iid"] to the id which we'd get from our database ($iid) and then including view.php, you can emulate a call to the file (and hence we'd print the view.php page for every item in the database).
If this is not what you want, it'd be awesome with some more info so we can help you along the way.

PHP URL Problem

I have a question in PHP.
I have a website thats hold news from mysql the (title, date, author, description) everything works fine, I have a page when which the user click on the title he go readmore.php?id=10 (for example)
Now my question is how to make to read the full story in one single page
for example index.php?id=10, I don't want to send in the readmore.php page, I want in one single page, how can I make this ?
check there is **id** querystring .
if(isset($_GET["id"]))
{
// show news which belongs to **id**
}
else
{
// list all news
}
But don't forgot validating $_GET["id"] variable. For example, if ids only numeric, you can check use *is_numeric();* etc.
It really depends on your current scripts but maybe you can modify the readmore.php page to only send the body of the page and then require ("readmore.php"); in your index.php where you need the full story, if the parameter (id) is the same. Then you can use something to show/hide the full body of the news in a div, in example.
you can use a jquery plugin for that, for example jQuery Expander Plugin
maybe you could send a get variable like: index.php?id=10&full=1. So, if full == 1 in index.php yo can show all the news, else cut the news with substr()
url like so:
index.php <- normal news page
index.php?readstory=10 <- will display story with id 10
in index.php
if(isset($_GET['readstory'])) {
$story_id = $_GET['readstory'];
display_story($story_id);
}
else {
display_news();
}

How to paginate wordpress page + need help debuging + need help with POST/GET requests

This is the page, its a wordpress powered site:
http://bit.ly/9oJXWV
You select some value, it makes POST to same page and based on value you selected it makes a list pages.
Now before you jump into my code i just want to say that im a newbie and that my main problem here were database queries so i didnt focus on other small stuff(like bunch if's at start inline css and stuff like that).
So this is my template:
http://pastebin.com/HQvMq3Db
This is a function from functions.php which im using in template:
http://pastebin.com/fWKqqzQv
This page works the way i want it and i just finnished putting all the code together but have one issue. Once i get that sorted out i will make the code a lot nicer... :)
So the issue is that if you look at pages that are listed once you make a selection and submit, on a lot of them some values are missing even thought those values are there(open any page from that list which is missing some value and you can pretty much see the same stuff but now it display's all the data).
So that is the part i need help with debugging. I really have no idea how to tackle this.
Second part of this question is simple: how do i paginate this page? Any link, tip, tutorial would be good.
Also one more thing, how can i have links for example like this:
.../hostels/?grad=Beograd
and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST? O_o Not sure what to do here, like i said im newbie.
Thanks for reading, looking forward to answers and comments.
Cheers!
You can set up your functions to enable pagination in WP without have to do any custom logic.
See: http://codex.wordpress.org/Template_Tags/query_posts#Pagination_Parameters
and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST?
yes. yes. no.
GET requests retrieve the variables from the url. so you just run a link with GET variables, the php would suscessfuly display your info. but if you are using POST, the variables are retrieved from "the background", passed by the previous page. so you cannot just run a link, the page must be called from a previous page (trough a form) or the page won't have access to the variables.
1) I fixed pagination simply by implementing &paged='.get_query_var('paged') to my query. Now it looks like this:
$hostels = new WP_Query('post_type=page&meta_key=Grad&meta_value='.$grad.'&posts_per_page=60&orderby=title&order=ASC&paged='.get_query_var('paged'));
#js1568 i gave him +1 for his answer, but he didnt answer my entire question.
Now i can go through pages like so:
/acommodation/hostels/?city=beograd - this is page 1
/acommodation/hostels/page/2/?city=beograd - this is page 2
/acommodation/hostels/page/3/?city=beograd - this is page 3
etc...
2) The issue with missing info from some pages is fixed by putting this below the end of inner loop:
wp_reset_query();
and also i created some custom function which will get all meta values for given post id:
function custom_get_meta_values($id){
$first_array = get_post_custom_keys($id);
foreach ($first_array as $key => $value) :
$second_array[$value] = get_post_meta($id, $value, FALSE);
foreach($second_array as $second_key => $second_value) :
$result[$second_key] = $second_value[0];
endforeach;
endforeach;
return $result;
}
In my inner loop i call that function like this:
$result = custom_get_meta_values($post->ID);
Then i just echo what i need like so:
echo $result['Mail'];
Just put the name of meta field in that $result array and echo it.
3) I replaced POST with GET request so now i can have links like this:
/acommodation/hostels/?city=beograd
which when opened will show every hostel from 'beograd'. I only have 4 possible values for cities so if value of 'city' that i capture from GET request is not one of those 4 values, i do nothing, just show that form. If it is i take that value and show the list from that city.
As per Will instructions, i will mark this answer as accepted.

Categories