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();
}
Related
I have a single page site on which I show a few lines of the 2 latest news on the start page. When you click the read more link, the page should scroll to the news section and there you should read the full news. The scroll part/show full news are ok but I have no idea how to put them together. This URL format is not working www.example.com?id=1#news. It either does not scroll or it just jumps to the news section. TX
EDIT: In the end I am using the data-elemid to pass the newsId variable to an ajax call. Everything works as it should now.
Try something like this:
$(function(){
var hash = window.location.hash;
if(hash != "") scrollTo(hash);
});
This will check if the current URL has a hash, if it does, then call the scrollTo() method (you should replace this with your own scroll method).
I'm looking to build a site which will have multiple options, for example;
NOTE** These are images not any forms or input type
Pick from one of the 5 images
Pick from one of the 3 images
Pick from one of the 2 images
Pick from one of the 6 images
and so on...
What I need to do is record which option (image) the user clicked on and then output a message.
For example if you picked a b c d, msg = hello
I'm not sure how to record this data, thanks!
You could link each image to a seperate page or reload the current page with a query string appended for example adding the following to your images as links
<img src="set1-image3.png" />
You could then save this information using PHP by processing only if the GET string contains the parameters image and set and could then split out which set/image has been clicked this way.
You could also use this to choose which image set to show on the screen (show set 2 if set=1 is in the query string etc.)
Using $_GET['set'] and $_GET['image'] you would have access to this information very easily in PHP.
For example you could do:
if( isset($_GET['image']) && isset($_GET['set']) ) {
echo "You clicked image" . $_GET['image'] . " from set " . $_GET['set'];
}
That would be the only pure PHP answer. The other way to do this is with Ajax or JavaScript but I'm guessing by your part about it being done in PHP this may not be what you want.
You could add a parameter to the link you click.
For example <img>
In page2.php you could read that variable using:
$_REQUEST["clickedImage"]
For example:
<p>The clicked image is: <?php echo $_REQUEST["clickedImage"] ?>
I've been Google-ing this for the past 24 hours, pretty much. I can't seem to find an answer that fits my scenario, even though I know it's widely used.
I have many links on one page (let's call it "page 1") of my website and I would like to them all to head to one, blank page (and this "page 2") and then have the content dynamically load, depending on what link was clicked on the previous page.
All I know is that I need to be able to set a PHP variable with the hyperlinks on page 1 and use some kind of if(isset($_SESSION['phpVariable'])){ include('path/content.fileExt')} on page 2 page so that it's ready to receive the variable and load the content accordingly.
I just have no clue how to do this, as I only started learning PHP yesterday. Help? :( :L
You could use the $_GET[] variable. On page1, the links could be to page2?food=bacon or page2?food=foo. On page2, you could say...
if ($_GET['food']=='bacon')
{
// Stuff here that prints what you want.
}
elsif ($_GET['food']=='foo')
{
// Stuff here that prints what you want.
}
You can pass an identifier of the content to be loaded through the URL.
Ex:
Set a value for one of the GET parameter to be the identifier for the content of page 2
Link to Page 2
And then in page 2 just check for the value and load the content accordingly.
include_once 'content/' . $_GET['pageid'];
http://www.w3schools.com/php/php_get.asp
www.example.com/index.php?page=something_here
$_get["page"]
Would return "something_here"
I have two php files which redirect like this
`header('Location: usrsecuredpage.php?div=subscription');`
`header('Location: usrsecuredpage.php?div=stusearch');`
On the "usrsecuredpage.php" I have a few javascrip toggle links on page which show and hide div sections. this is the code:
<li><b>Subscription Details</b></li>
<li><b>Personal Information</b></li>
clicking the subscription toggle link shows subscription detail and hides personal information and vise versa..
I would like the headers being supplied by php pages to automatically show relevant div section. for example when header header('Location: usrsecuredpage.php?div=subscription'); is received from php page i would like subscription detail section to toggle and show up..
how can i do this?
Add a query string parameter to your URL and have the page show or hide the div based on the query string parameter.
For example,
header('Location: usrsecuredpage.php?ShowDiv=true');
Then, in the resulting page, you can check the value of that variable by using the global variable $_GET, like this:
$_GET['ShowDiv']
Using this variable, you can show or hide the div accordingly. Let me know if I can provide any more detail...
I would recommend using jquery for this if you can ... Just include the jquery library and then use this code ...
$("#yourDiv").show();
Use GET parameters that you can pass through the URL. For example, in your PHP file which redirects back to the original page, type the following for your redirection code:
header('Location: usrsecuredpage.php?div=sub'); // div='sub' or div='stu'
Now, in the usrsecuredpage.php file, include the following code near the top to find out which div to have open at the start:
$openDiv = $_GET['div'];
At this point the $openDiv variable will contain either 'sub', 'stu', or will not be defined (if you didn't include the parameter in the URL). You can use isset($openDiv) to find out whether it is defined or not. If it is defined, then compare against the value of $openDiv to find out which div to have open at the start. For example:
// initialize display of both divs to none
$subscriptionVisibility = "none";
$stusearchVisibility = "none";
// now, based on value of $openDiv, set above vars
if (isset($openDiv) && strcomp($openDiv,"sub") == 0)
$subscriptionVisibility = "inline";
else if (isset($openDiv))
$stusearchVisibility = "inline";
.
.
.
// now, whenever you create the divs, use their visibilities
echo "<div class='jotform-form' id='subscriptionDiv' style='display:$subscriptionVisibility;'></div>";
echo "<div class='jotform-form' id='stusearchDiv' style='display:$stusearchVisibility;'></div>";
In this example if your parameter was anything other than 'sub', the code assumes it was 'stu'. So if your parameter was 'bla', it would still make the stusearch div visible.
This may not be the best code for this solution, however, it should be enough to push you in the right direction. The overall approach is: pass the parameter in the URL string as a GET parameter (as I have shown), and then, in the usrsecuredpage.php file, use that parameter to determine which div should be open from the start.
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.