Wordpress PHP if else - php

I have to say that I'm a newbie when it comes to php , but still this code seems to make sense. I'm trying to display a form in 2 different ways - one way if it's on the page 'cotatie-seo' and another way if it's on any other page in the site. The code I've used is:
<div style="position: relative; float: left;">
<p>Nume (required)<br />
[text* your-name] </p>
<p>Adresă Email (required)<br />
[email* your-email] </p>
<p>Adresă website (required)<br />
[text* Website]</p>
<?php if(is_page('cotatie-seo')) { ?>
</div>
<div style="position: relative; float: left;">
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
<?php } else { ?>
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
<?php } ?>
<p>[submit "Trimite"]</p>
If it's not on the cotatie-seo page it displays the form properly, with the text area under the rest of the fields, but when it's on the cotatie-seo page the text area displays both in the first div and in the second div (so it is displayed twice, both under the rest of the fields and next to them).
Thank you in advance for the help!
Thanks for the fast reply! The function is the regular Wordpress is_page function:
function is_page( $page = '' ) { global $wp_query;
if ( ! isset( $wp_query ) ) { _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' ); return false; }
return $wp_query->is_page( $page ); }
I'm sorry but I can't post images and I'm building the site on localhost, so I can't provide a preview of the output.
Cheers!

One thing to try would be moving this bit:
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
outside of the if-else (to just above the submit), and then dropping the else entirely since you repeat these lines in both if and else. Your snippet above would then be:
<div style="position: relative; float: left;">
<p>Nume (required)<br />
[text* your-name] </p>
<p>Adresă Email (required)<br />
[email* your-email] </p>
<p>Adresă website (required)<br />
[text* Website]</p>
<?php if(is_page('cotatie-seo')) { ?>
</div>
<div style="position: relative; float: left;">
<?php } ?>
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
<p>[submit "Trimite"]</p>

Related

Wordpress, Redirects and WPML

I have a fresh installation of wordpress, and I wrote some quick rewrite rule in my functions.php that looks like this -
// "rewrite" /assets/xyz to /assets/?asset=xyz
function assets_rewrite_rule($rules) {
global $wp_rewrite;
$asset_rule = array( // (not tested)
'assets/(.+)/?' => 'index.php?pagename=Assets&asset=$matches[1]'
);
return array_merge($asset_rule, $rules);
}
add_filter('page_rewrite_rules', 'assets_rewrite_rules');
And I have my sidebar translated with WPML and String Translation in the most primitve way.
Here's the english part (not translated) of my sidebar -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/assets/first-asset/">Review</a>
<a class="btn-sidebar1" href="/buy/first-asset">Trade Now!</a></div>
<div style="clear: both;"></div>
</div>
And while trying to make a translation to other language (let's say italian), the wpml refuses to save my changes to the review links..like my redirection rule is affecting it somehow.
Here's the translation I added to the sidebar -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/it/assets/first-asset/">Revisione</a>
<a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div>
<div style="clear: both;"></div>
</div>
As you can see both the review and the buy links were changed.. but after I hit save, it only saves the change i made in the buy href, but it reverts my change to the review link, and it looks like this after I save -
<div class="sideall">
<div class="sidebar-mai1">
<div class="sidebar-name">
<img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div>
<div class="sidebar-btn">
<a class="btn-sidebar" href="/it/assets">Revisione</a>
<a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div>
<div style="clear: both;"></div>
</div>
As you can see, after I hit save, it removes the /first-asset part from my translation and now it leads to an empty page (/it/assets) .. I am wondering if it may be cause as a result of the rewrite..
An alternate way to approach this:
Listen to your customer's browser setting.
locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
https://secure.php.net/manual/en/locale.acceptfromhttp.php
You can then rewrite it to a function like this:
function my_get_langauge() {
static $lang;
if(is_null($lang)) {
$lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
/**
* Check list of allowed/accepted languages or revert to default
*/
if(!in_array($lang, ['nl','en','de','it']) )
{
$lang = 'en';
}
}
return $lang;
}
This way you don't have to worry about redirects for languages, and you can accept languages as your website user wants to see it.
If you test your expression online you'll see that your regex is removing the first-asset part of the URL.
Regular Expression: assets\/(.+)\/?
Test String: /it/assets/first-asset/
This will return /it/assets/.

print div With unspecified background html,javascript,php

I have a project for kids cards where the child selects the background from specific backgrounds and write his name and print
But the problem is that I only want to print the card (apDiv14 with background) not the full page Or if there was a way to convert certain content to pictures for print
<div id="apDiv14">
<div id="for1">
<h1 ID="head1"> <br>
<h1 ID="head2"> <br>
<h1 ID="head3"> <br>
<br>
</h1>
</div>
<div id="apDiv17"><form name="form1">
<p>
<br>
<input type="text" name="newtitle" size="25">
</p>
<input type="text" name="newtitle2" size="25">
<br>
<br>
<select name="newtitle3" style="width: 150px;">
<option value="">age</option>
</select>
<br>
</p>
<p> <img src="img/BB.gif" width="80" height="45" onClick="ChangeTitle();"><img src="img/PB.gif" width="80" height="45" onClick="pri();"> </p>
<p>
</form> </div></div>
<script>
function pri() {
window.print(); }
</script>
function pic1(apDiv14)
{
// background 1
document.getElementById("apDiv14").style.backgroundImage="url('img/1st card Jeddah.jpg')";
document.body.style.zIndex='4';
document.getElementById("apDiv14").style.backgroundSize = "527px 307px";
document.getElementById("apDiv14").style.backgroundRepeat='no-repeat';
}
function pic2(apDiv14)
{
//background 2
document.getElementById("apDiv14").style.backgroundImage= "url('img/2nd card Jeddah.jpg')";
document.body.style.zIndex='4';
document.getElementById("apDiv14").style.backgroundSize = "527px 307px";
document.getElementById("apDiv14").style.backgroundRepeat='no-repeat';}
You can use a print media stylesheet to limit which content gets printed (by setting other content to display: none but most browsers are configured to not print background images (since they are rarely important and use a lot of ink).
I'd approach this by using some server side code to generate a PDF version for printing and using a form to submit the options to the server.
javascript code:
var content = document.getElementById("divcontents");
var pri = document.getElementById("ifmcontentstoprint").contentWindow;
pri.document.open();
pri.document.write(content.innerHTML);
pri.document.close();
pri.focus();
pri.print();
html code:
<iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"></iframe>

Why hidden input is affecting my layout? LF Explanation

I needed to use an hidden input to transfer some IDs to the page for each block.. whatever.
I have the following code :
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<div class="span1">
<input type="hidden" name="tracking_id" value="" />
</div>
</div>
</div>
This code work well and the result is what I expected.
If I do this :
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<input type="hidden" name="tracking_id" value="" />
<div class="span1">
</div>
</div>
</div>
The layout is not respected. See this picture for the demostration :
Can someone explain why to me ? Hidden input aren't suppose to be "hidden" so they shouldn't affect the layout ?
jsfiddle : http://jsfiddle.net/t9M3C/
Near line 285
Is because you have a css rule (in bootstrap.min.css file) that match the firs-child element (but only if has a class*="span") inside the default_shipping_box div.
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
So, if you put your hidden input inside the div#default_shipping_box and before the first span, then that rule is not styling the div.span1 and thats why your template is been afected.
You can fixed adding a simple css rule to the same file...
.row-fluid .span1{margin-left:0 !important;}
The important, is because you have more files who overite this rule (ex. in bootstrap-responsive.min.css)
Good luck, and i hope it helps
cheers,
Gmo.-
EDIT:
Too slow XD.
Answered while writing ... I agree with the reason explained above.
Using Google Chrome's Inspest Plugin, when you move the input this class:
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
Gets removed.
This is because in this:
<div id="shipping_box" class="formSep well">
<div id="default_shipping_box" class="shipping_box row-fluid">
<input type="hidden" name="tracking_id" value="" />
<div class="span1">
</div>
</div>
</div>
This : <div class="span1"> is not the first child, this: <input type="hidden" name="tracking_id" value="" /> is.
and in your CSS this is that default class for [class*="span"] is:
[class*="span"] {
float: left;
margin-left: 30px;
}
So use this for example :
.row-fluid .span1 {
margin-left:0 !important;
}
Hope this helps.
Bootstrap has some CSS that will set the left-margin of the first of the child to 0, if the class contains span:
.row-fluid [class*="span"]:first-child {
margin-left: 0;
}
When the hidden input is put above the first span div, the above margin-left: 0; property will not be applied.
The following image shows that when the hidden input is before, then the first span class has a left-margin.
This shows that when the hidden input is after the div, that there is no left-margin.
EDIT: I seem to have a been beaten twice, while I was getting the screenshots to illustrate the difference!

How to let the user choose a page number?

I've added information to some boxes, which are alligned with the 'float' attribute.
The content of all those boxes together is too big to fit in the webpage, so I want to put those boxes on different pages, and to let the user choose which page he wants to view.
E.g: Choose page: 1 2 3... (last page)
The contents of these 'pages' must be in divisions.
For reference, see my css code:
.clear {
clear: both;
}
#over-ons .imagebox,
#over-ons .contentbox
{
background-color: #C4C4A6;
float: left;
margin: 0 10px 40px 0;
padding: 10px;
border: 2px solid black;
}
#over-ons .imagebox {
width: 100px;
text-align: center;
}
#over-ons .contentbox {
width: 200px;
}
Also, see my HTML code of the 'boxes' I mentioned:
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Function: blablablabla<br />
Name: blablabla<br />
City: Breda<br />
Education: HBO Informatica<br />
</div>
<div class="clear"></div>
How do I create the ability to let the user view 3 different boxes, depending on the page number he wants to see?
Edit:
My current solution looks like this, only nothing happens when I click 'page 2':
<?php
//Vraag pagina op
$page = 1;
if (isset($_POST['page']) && is_numeric($_POST['page']))
$page = $_POST['page'];
?>
<? if ($page == 1) { ?>
<div class="imagebox"><img src="media/images/overons/pieter.png" /></div>
<div class="contentbox">
Functie: Bedrijfsleider<br />
Naam: Pieter<br />
Stad: Deurne<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/patrick.png" /></div>
<div class="contentbox">
Functie: Verkoper<br />
Naam: Patrick<br />
Stad: Roosendaal<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/rick.png" /></div>
<div class="contentbox">
Functie: Ontwikkelaar<br />
Naam: Rick Gommers<br />
Stad: Oosterhout<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else if ($page == 2) { ?>
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Functie: Salesmanager<br />
Naam: Bas<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/robbert.png" /></div>
<div class="contentbox">
Functie: Administrateur<br />
Naam: Robbert Tuerlings<br />
Stad: Goirle<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/timothee.png" /></div>
<div class="contentbox">
Functie: Implementatie Technicus<br />
Naam: Timothee<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
Imagine your current page is called index.php. So first of all you want to create the links to your pages in your HTML as the following:
Page 1
Page 2
Page 3
And for a clear code you can simply put the content of your "boxes" in an array and then output the corresponding entry for the page:
<?php
$content = array{ "Page 1 data", "Page 2 data", "Page 3 data" };
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']))
$page = $_GET['page'];
?>
<div class="contentbox">
<? echo $content[($page - 1)]; ?>
</div>
Another solution (less easy to read and to maintain, but easier if your content is really complex) you can simply fetch the page number and in a regular if else display the box they requested.
<?php
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0)
$page = $_GET['page'];
?>
<? if ($page == 1) { ?>
<div class="contentbox">
Your first box for page 1
</div>
<? }
else if ($page == 2) { ?>
<div class="contentbox">
Your second box for page 2
</div>
<? }
else if ($page == 3) { ?>
<div class="contentbox">
Your third box for page 3
</div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
But ye, there are 1001 ways to do this. It really depends on your needs and the way you want to do it. The more you hardcode, the uglier the code usually gets.. ;-)
One way is to have all divisions on the page and make only the one visible which user has clicked to...To do so you can make all 3 divs and by default make them hidden and by javascript trigget action...
Another way is by PHP's output buffering to accomplish this:
ob_start(); // begin collecting output
include 'myfile.php';
$result = ob_get_clean(); // retrieve output from myfile.php, stop buffering
$result will then contain the text..
The first method is preety easy one most people use it..But as you specificaly said you have created all separate pages for all 3 div's I had to add the php method..

how to generate html with php to return after ajax call

I generate all my html on my page with ajax calls and never refresh my page. My setup is something like
HTML: <a id="user_click_here">test link</a>
JQUERY:
$("#user_click_here").live("click", function(event){
console.log("click on test link");
ajax_function("action=return_some_html");
});
The ajax function calls php, where I create the html. What I struggle with is to generate html with php. I try to use this:
PHP:
$html = <<<HTML
<div class="box">
<h2>
$text[0]
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
HTML;
$html .= '<p>Username: <a id="username">' . $_SESSION['username'] . '</a></p>';
$html .= <<<HTML
<div style="float:left; width: 30%; margin:5px;">
<p>
Level:<br />
Weapon:<br />
Power:<br />
Bullets:<br />
</p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p>
HTML;
$html .= '<b id="level">empty</b><br/>';
$html .= <<<HTML <---ERROR HERE unexpected '<<'
Weapon blabla<br />
2 - 5<br />
3/6<br />
</p>
</div>
</div>
I tend to just try and fail until it works with this <<<WHATEVER (dont remember what it's called). Like now where I get an unexpected '<<' error for some reason.
Do I have to use this method:
$html = '<div class="box">
<h2>
' . $text[0] . '
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">';
?
What is the best way to save html in php and send it back to jquery.
I send the html like this now:
PHP:
$data = array("html" => $html);
return json_encode( $data );
Off course I want it compressed as much as possible, preferably without any stuff like this: \n\t\t\t\t to take up space.
EDIT:
Ok. I don't think everybody noticed the fact that this is an ajax call and I have to return a json element. I cannot do the common <?php php code ?> html code <?php more php ?> some html
It looks like your last HEREDOC (that's what the <<< syntax is called) is unclosed, missing it's
HTML;
And don't forget that the final HTML; cannot have any whitespace on the same line before or after it.
However, you're going about it all wrong. The great thing about the HEREDOC syntax is that you can embed all your variables into it without requiring any concatenation. Just create the whole thing in one HEREDOC and echo it out to jQuery. There's no need to make it JSON if you are just going to use it as HTML when received by the AJAX call.
All your code above belongs inside one $html = <<<HTML block. Enclose all your complex variables like $_SESSION['whatever'] in {} {$_SESSION['whatever']}.
$html = <<<HTML
<div class="box">
<h2>
{$text[0]}
</h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
<p>Username: <a id="username"> {$_SESSION['username']}</a></p>
<div style="float:left; width: 30%; margin:5px;">
<p>
Level:<br />
Weapon:<br />
Power:<br />
Bullets:<br />
</p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p>
<b id="level">empty</b><br/>
<!--etc -->
<!--etc -->
HTML;
// Now just echo it back to the AJAX caller as HTML
echo $html;
exit();
Don't use HEREDOC. It's messy, and should only be used when you need to store long strings in a variable.
Try writing out the HTML page you expect to be returned as if it was a normal HTML page. Where you need PHP output (a variable, array value, etc.), begin a PHP tag, echo the value, and then end the PHP tag. This is much easier than trying to use heredoc.
<div class="box">
<h2><?php echo $text[0]; ?> </h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
<p>Username: <a id="username"><?php echo $_SESSION['username']; ?></a></p>
<div style="float:left; width: 30%; margin:5px;">
<p>Level:<br />Weapon:<br />Power:<br />Bullets:<br /></p>
</div>
<div style="float:right; width: 60%; margin:5px;">
<p><strong id="level">empty</b><br />Weapon blabla<br />2 - 5<br />3/6<br /></p>
</div>
</div>
If you're worried about line breaks (you shouldn't be), you can add an output handler that removes them.
ob_start("clean_linebreaks");
function clean_linebreaks($input) {
return str_replace("\n", "", str_replace("\r", "", $input));
}
Also, may I suggest using tables instead of floated divs. The level, weapon, and power will be easier to line up with their values if you use a table.

Categories