PHP show image if post result equals - php

I have a form that posts to a page. I want to display an image if the value of the item posted equals "paypal".
I need to write something that says;
if $_POST['method'] equals "paypal" then show paypal.gif
if $_POST['method'] equals "mastercard" then show mastercard.gif
I hope I made a bit of sense, new to php trying to learn the best I can

If the methods have the same values as the file names, you can use this:
<img src="<?=$_POST['method']?>.gif" />

Related

how to get argument value from navigator_bar.php?type=1?

I have a below like index.php:
index.php:
include_once('navigator_bar.php');
include_once('content.php');
include_once('footer.php');
my navigator_bar.php just list different product type from database, I loop it from DB and echo they as a link like:
navigator_bar.php?type=1
navigator_bar.php?type=2
navigator_bar.php?type=3
...
my content.php just show different product list from navigator_bar.php retrieve from DB, that's, when user click type one button(navigator_bar.php?type=1), then content.php show type=1 products; when user click type two button(navigator_bar.php?type=2), then content.php show type=2 products.
now my question is : I can't get the parameter "type" value from the content.php, because both navigator_bar.php and content.php are all included in index.php, they are all in the same page, can't get it via "$_GET['type'],
and they are all contained in div not in frame, still not in form, so I can't get they from a form submit; of course, I can define "type" as a variable in navigator_bar.php, get it from content.php,
but please note that, my type value loop from database, it's scope just in navigator_bar.php, when get it from content.php, I can just get the last value of it.
but my content.php need to refresh different products from the argument value like navigator_bar.php?type=value, anyone have good suggestions for how to get the argument value from the include navigator_bar.php ?
regards & thanks
David
Try set a $_SESSION['type'] in "navigator_bar.php" with the value of type (where you can get that value), then get and use it from session in the other php file.
Both files must have this code at beginning:
if(!isset($_SESSION)) session_start();

How to record what a user clicks on

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"] ?>

Display image if post's custom field has specific value

How can I display certain image in a post if that post contains a specific custom value.
For example, I have two posts, one has a custom field named top_post and its value is true. The other one doesn't have that value at all. I want the one with this value set to true to have image displayed (an image that shows it's a top post) and the one without the value to show nothing.
I know that the function would be something like "if top_post value is 'true' then display img(url)" but I don't know how to write that function as I'm just a PHP beginner.
Any help appreciated.
EDIT: I tried finding the function code on various coding blogs but didn't succeed... Also, I have no function coding experiences and that's why I was asking for a function code.
To expand on Atiaxi's answer, and perhaps make it a bit more readable for you as a beginner in php:
if ($top_post == true)
{
echo "<img src='top_post_image.png'>";
} else {
echo "<img src='blank.png'>"; //you could also leave this line out if you want 'nothing'
}
You're on the right track, I think what you need to keep in mind is that you're using PHP to generate the HTML on the page. So if you want an image to show up if a certain condition is true, you only generate the HTML for that image if the condition is true:
if(top_post)
echo "<img src='top_post_image.png'>"

How to control the content (text) to display in a $form->input?

folks.
I'm starting with CakePHP and after reviewing this tutorial (
http://book.cakephp.org/view/1543/Simple-Acl-controlled-Application
) and also after having used the "cake bake" command to generate my
models,
controllers and views , everything is fine, but when I visit the
Post's add view (views \ posts
\ add.php), I find that instead of showing a input text for the
username, it shows a select with all the usernames.
this is the line in the Post's add view that show the select.
echo $this->Form->input('user_id');
PostsController :
function add() {
// Some other code
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
Although I know how to display only the username of the currently
logged-in user, I don't know how to control the content to show in
$this->Form->input() because if I use a variable that is not part of
the "Post" model , it's shown , but as the label for the input.
Have you any idea how to solve this??
P.S. I've been trying to find this information on the cookbook , but I haven't been able to find anything specific to my situation :(
If you want to add a string in as the value of the input, try this :
echo $form->input('my label text',array('value'=>'the value string','type'=>'hidden'));
That would make the input hidden, and set the value.
Or remove the type=>hidden and it will have a value, you can also add things like disabled and such in there, or to modify the div it creates, use div'=>array() and put your div options in that array... hope this helps.
i would like to help you on this thing.
first of all when you want a text box then you must define the type of the input field.
echo $form->input('user_id', array('type'=>'text', 'label'=false));
i hope this will help you.

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