Highlight current value in loop menu - php

i'm trying to highlight current value in this loop menu:
<?php
for ($i=count($anos)-1;$i>=0;$i--)
echo '<li>'.$anos[$i]['ano'].'</li>';
?>
I'm trying to fix some problems in a web page but my knowledge in programming is rudimentary at best. I've managed to fix a lot of issues by googling, but i'm having trouble getting a solution to this.
This loop gets the years (anos) from the DB and displays all of them in a menu, clicking on a year brings a page with the image gallery.
This is the page: http://marialealdacosta.com/obra.php
Thanks for the help.

You can use $_GET to use the passed value along with URL. In your case it's ano.
Use this value to compare with loop's value and if they are similar change the style of the element.
So your code look something like this,
<?php
$ano="";
if(array_key_exists("ano",$_GET)) {
/*
Check if ano value is passed along with URL
before accessing it. Other wise it will give error.
*/
$ano=$_GET["ano"];
}
for ($i=count($anos)-1;$i>=0;$i--) {
$styles="";
if($anos[$i]['ano']==$ano) { // this will check the current ano.
$styles="background:red;"; // change appropriate styles here...
}
echo '<li style="'.$styles.'">'.$anos[$i]['ano'].'</li>';
}
$_GET is an associative reserved array of variables passed to the
current script via the URL parameters.

Related

php or jquery for dynamically created html pages?

i have over one hundred html files that all roughly have the same structure. the html files represent each page of a comic.
naturally, i've realized that it would be more efficient to apply the structure of these pages to one file, and allow that file to dynamically create all of the comic pages.
my goals are so that, one; when a user accesses a comic page, the page number that the link contained can be passed through as a number value to determine the image file and the url.
two; when a user clicks "next" or "previous", the number value that defines the page is either added to or subtracted by one to load in the new url & image file.
and three; for the "next" and "previous" buttons to define themselves/decide whether to appear or not based on a filecheck i've already written.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
<html>
<div class="block">
<?php echo '<img class="page" src="' . $imgnum . '.png">'; ?>
</div>
</html>
this code does the trick for changing the image file and url based on the value of $imgnum but when it comes to passing values into the "next" and "prev" buttons, i have no idea what the hell im doing. i tried doing THIS:
<?php $prevValue = $imgnum - 1?>
<h2 class="arrow">Previous</h2>
clearly, passing a variable into that key does not work. is there any way to preserve the dynamism of the buttons with this php method? these buttons need variables to work on their own. do i have to switch to jquery? i honestly above all else need advice on where to start. i barely understand what's going on in this php script itself.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
What the above does.....
If (the URL string contains the [img] value ) {
Get the value of [?img=] from the URL string...
i.e. index.php?img=4 .. (it is getting the 4)
and set the [$imgnum] variable to that value
} else {
But if the URL does NOT contain the [img=] value,
then set the variable value to 1 [$imgnum = 1;]
}
It's difficult to tell exactly what you want, but the below will add the value properly to the button.
<?php $preValue = $imgnum - 1; ?>
<h2 class="arrow">Previous</h2>
PHP lines end with a semicolon... and you have to echo variables for them to be output to the HTML.
I have no clue what jQuery and javascript have to do with your question. But yes.. this could all be handled by an ajax request and you wouldn't need to reload the page every time a button is clicked.
However, if you don't understand what the PHP is already doing, explaining how to do this via AJAX will be exceptionally confusing to you. And much more of your code structure would need to be seen.
You're creating the right code but you just forgot to echo it.
You need to change your line to;
<h2 class="arrow">Previous</h2>
or you can use the shorter version if your php.ini configuration allows to;
<h2 class="arrow">Previous</h2>
Note:
You should be aware of SQL injection and secure your get variables by;
$imgnum = (int)$_GET["img"];
As you are taking the $_GET value as a variable, this time, only integer values will work so hackers cannot inject a code.

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'>"

show/hide div section based on where the page is redirected from?

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.

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.

Value of variable changes once out of the click function in jQuery

this is the continuation of my previous question. Since I just logged in that day without an Open Id, I don't know how to login to that account and edit further. So I created a new Open Id enabled account and posted this as a new question.
Now I have the code like this. Inside the onclick event, the value is stored in the $selectedId correctly. But When I try to pass that value in the url, I do not get the correct value. The last value in the for loop is passed.
<script type="text/javascript">
$(document).ready(function(){
<?php foreach ($Forms as $r): ?>
$("<li><a id='<?=$r['Form']['id'];?>' data-attr='Formentries' href='#'><?=$r['Form']['name']?></a></li>").appendTo("#headers").click(function () {
<?php $selectedFormId=$r['Form']['id'];?>
alert("selId: "+<?php echo $selectedFormId;?>); //here the selected id is alerted
});
alert("outside the loop"+<?php echo $selectedFormId;?>); //here the last value in the loop is alerted
});
Once out of the click function, the value of $selectedFormId changes to the last value in the array. Can someone help me with this?
Actually what I am trying to achieve is, I list a set of Forms as links, and when I select the links I want its id to be saved in a php variable. I want it particularly be saved in a php variable coz after I select a Form I have an option to export the entries in the form through another link
<a href="localhost/FormBuilder/reports/export/<?php echo $selectedFormId;?>" class="thickbox button" title= "Export" >Export</a> .
So I want the id in there,so that I could pass it to the export function in the controller.
I also get the selected id in a javascript variable as
formid=$(this).attr("id");
but I do not know how to pass this value to the export function in the controller.
I don't know if i've understood your question very well but the $selectedFormId value is set inside the loop, so everytime the loop is executed the variable is set and when the loop finishes $selectedFormId gets the last processed value. I think you should set it outside the loop.
as you are declaring it inside the block so it is not available outside;
either you declare it before the $(document).ready block or finish its usage it inside this block itself.
As far as I can see you are setting $selectedFormId inside each loop in the iteration, so the assignment code will only be set when the click event is fired.
Therefore, logically, when you exit the loop, $selectedFormId will be the last item in the array. Are you meaning to put a condition around the assignment? Don't forget, the server side code will execute regardless! It won't care about client side conditions or closures!
if (something){
<?php $selectedFormId=$r['Form']['id'];?>
}
And, again echoing the above comments, you should really be trying to achieve code separation. The above really is tag soup!
You are mixing client and server-side code -- the code you wrote seems like you're expecting a JavaScript function on the client-side to magically set a PHP variable on the server-side, when in reality that type of operation is not possible.
What you should maybe do is have your click event set a variable on the client-side. Then set an onclick on your export link to construct a URL and redirect to it based on the locally stored variable value.
That's probably not the best solution, but it would be one option.

Categories