checkboxes condition is not working properly - php

I'm getting data from this function, now I want checked checkboxes based on my condition and my condition is if I got value roro from db no will be checked if I got Vessel, Containerized yes will be checked.
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = "checked";
} if($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = "checked";
}
and I'm using this condition in checkboxes
$html .= '<tr>';
$html .= '<td colspan="3">';
$html .= '<small>11. TYPE OF MOVE</small> <br />';
$html .= $getDataForPDF["typeOfMove"];
$html .= '</td>';
$html .= '<td colspan="2">';
$html .= '<small>11a. CONTAINERIZED (Vessel only)</small> <br/>';
$html .= '<input type="checkbox" checked="'.$checkedYes.'" name="yes"> Yes <input type="checkbox" checked="'.$checkedNo.'" name="no"> No';
$html .= '</td>';
$html .= '</tr>';
my condition is right but not working on checked="checked" condition. What is wrong in my code?

The checked property is as simple as adding checked at the end of the tag. For example <input type="checkbox" name="yes" checked> would be a checked box where the absence of checked would be unchecked.
The following code should solve the problem for you:
$checkedYes = '';
$checkedNo = '';
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = " checked";// note the space before checked
} if($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = " checked";// note the space before checked
}
$html .= '<tr>';
$html .= '<td colspan="3">';
$html .= '<small>11. TYPE OF MOVE</small> <br />';
$html .= $getDataForPDF["typeOfMove"];
$html .= '</td>';
$html .= '<td colspan="2">';
$html .= '<small>11a. CONTAINERIZED (Vessel only)</small> <br/>';
$html .= '<input type="checkbox" name="yes"'.$checkedYes.'> Yes <input type="checkbox" name="no"'.$checkedNo.'> No';
$html .= '</td>';
$html .= '</tr>';
JSFiddle: http://jsfiddle.net/o984L61e/
Edit
Try changing the top code block to:
$checkedYes = '';
$checkedNo = '';
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = " checked='checked'";// note the space before checked
}
if($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = " checked='checked'";// note the space before checked
}
The examples on tcpdf use the checked='checked' property just like the other answers had, but you must initialize the variables before you try to concatenate in the html string. As written in the question, either $checkedYes or $checkedNo will be undefined since they are being initialized in mutually exclusive if blocks. Not sure if this will make a difference but it seems plausible.

Could you try?
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = "checked='checked'";
} elseif($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = "checked='checked'";
}
Along with:
$html .= '<input type="checkbox" '.$checkedYes.' name="yes"> Yes <input type="checkbox" '.$checkedNo.' name="no"> No';
I'm not sure it'd solve your problem but that is what you want to do in order the check the checkboxes accordingly to your database info.
Does it help?

TCPDF's writeHTML() function only supports a limited subset of HTML tags. Check the writeHTML() documentation for details. The input tag is apparently one that is not supported.
Instead, you could use HTML entities to represent the checked and unchecked checkboxes. There's already a StackOverflow question that gives examples of possible entities you could use.

Related

Shortcode is not working correctly if it is used more then one time in a same page

I have created one shortcode to return a tab content and it is working fine
if I am using one time in a page.But if want one more on this same page it is not working. I have tried in several way but its not giving fruits.
Here is the code
if($tab_box == 'tab_box_1' && $tabs_lay == 'awavc-tabs-pos-left') {
$add_class = rand(99,9999);
$q = rand(99,99999);
$html .= '
<div class="awavc-tabs awavc-tabs-'.$add_class.' '.$tabs_lay.' '.$tab_style.' awavc-tabs-response-to-icons '.$el_class.' ">';
$i = 1;
foreach($tab_contents as $tab_content){
$tab_lable = 'Lable';
$tab_icon = '';
if(!empty($tab_content['tab_lbl'])){$tab_lable = $tab_content['tab_lbl'];}
if(!empty($tab_content['icon'])){$tab_icon = $tab_content['icon'];}
$html .= ' <input type="radio" name="awavc-tabs" checked id="awavc-tab-'.$i.'" class="awavc-tab-content-'.$i.'">
<label for="awavc-tab-'.$i.'"><span><span style="font-size:'.$lable_size.'px;color:'.$lable_clr.';"><i class="'.$tab_icon.'" style="font-size:'.$lable_size.'px;color:'.$icon_clr.';"></i>'.$tab_lable.'</span></span></label>';
$i++;
}
$html .= '
<ul>';
$i = 1;
foreach($tab_contents as $tab_content){
$tab_title = 'Title';
$content = '';
if(!empty($tab_content['title'])){$tab_title = $tab_content['title'];}
if(!empty($tab_content['content'])){$content = $tab_content['content'];}
$html .= '<li class="awavc-tab-content-'.$i.'">
<div class="typography">';
if(!empty($tab_title)){ $html .= '<h1 style="font-size:'.$ttl_size.'px;color:'.$ttl_clr.';">'.$tab_title.'</h1>';}
$html .= '
<p style="font-size:'.$content_size.'px;color:'.$content_clr.';font-style:'.$content_style.';">'.$content.'</p>
</div>
</li>';
$i++;
}
I have tried something like $i.$add_class but...
If you want to use the shortcode multiple times on the same page do it like so:
add_shortcode("fmg_shortcode", "fmg_callback");
function fmg_callback($args) {
ob_start();
$html = "";
//your code here
echo $html;
return ob_get_clean();
}

Why does this block my PHP code?

The query is
$q1 = "SELECT * from `event` ORDER BY `upload_date` DESC";
This is the function
function display_content($r, $dis){
foreach($r as $part) {
$dis .= '<br /><div id="event_box">';
$dis .= '<div id="event_name"> '.$part['name'].'</div>';
$dis .= '<div id="start_date"><b>From: </b>'.$part['start_date'].' </div>';
$dis .= '<div id="end_date"><b>Until: </b>'.$part['end_date'].' </div>';
$dis .= '<div id="event_link"><b>Link: </b>Event Website</div>';
$dis .= '<div id="event_venue"><b>Venue: </b>'.$part['venue'].' </div>';
$a = array(854, 480);
$b = array(300, 300);
$str = $part['embed_code'];
$partVideo = str_replace($a, $b, $str);
$dis .= '<div id="event_video"> '.$partVideo.' </div>';
$dis .= '<div id="event_image"><a href = " http://'.$part['link'].'" target="_blank"><img src = "data:image;base64, '.$part['image'].'"';
$dis .= 'alt = "'.$part['name'].'"></a></div>';
$dis .= '<div id="details"> '.$part['details'].'<br /><br />';
$dis .= '<button class="share2 btn-primary s_twitter">Tweet!</button>&nbsp&nbsp';
$dis .= '<button class="share2 btn-primary s_facebook">Share on Facebook!</button></div>';
$dis .= '</div>';
$dis .= '<br />';
}
echo $dis;
} // close function display content
Later on I call
display_content($r1, $display);
Only the first instance (entry) of an event from the database appears, and it appears fine (name, venue, details etc), but all the other event entries do not appear at all. Like they are 'blocked' maybe.
However, they all appear fine if I remove the line
$dis .= '<div id="event_video"> '.$partVideo.' </div>';
Entries either have a
$dis .= '<div id="event_image"><a href = " http://'.$part['link'].'" target="_blank"><img src = "data:image;base64, '.$part['image'].'"';
$dis .= 'alt = "'.$part['name'].'"></a></div>';
or a
$dis .= '<div id="event_video"> '.$partVideo.' </div>';
'$dis' or what '$dis' represents in the function is defined/instantiated ($dis = '';) outside of the function so should be a global variable.
What is going on and how can I fix it?
I know that they don't all have the same image / video information, which probably means I should normalise my database better (these entries are NULL in the database until I fill them), but shouldn't they all appear / print on screen anyhow?
Why is only the first event appearing?
Thanks!
'<div id="event_video"> '.$partVideo.' </div id="event_video">';
That's not how you close an HTML tag...You should be using:
'<div id="event_video"> '.$partVideo.' </div>';
Double check your code, and make sure you have proper HTML markup. You've done this in multiple locations.
Also, you may want to do a var_dump( $partVideo ) to verify whether or not you can actually concatenate that variable with the string, and to ensure that it isn't responsible for "breaking" the rest of your code.

pre tags in tinymce adding br or p tags on ever line

I have been struggling to get the pre tags to work in tinymce.
For instance if I use the following code:
if($getcats_num > 0){
$catlist .= '<select name="bcats" id="bcats" class="selectboxit">';
$catlist .= '<option value="false">Select Blog Category</option>';
while($fetchcats = $getcats->fetch()){
$catlist .= '<option value="'.$fetchcats['id'].'"';
if(isset($_GET['mode']) && $_GET['mode'] == 'edit'){ if($fetchcats['id'] == $bcats){ $catlist .= ' selected'; } }
$catlist .= '>'.$fetchcats['name'].'</option>';
}
$catlist .= '</select>';
} else {
$catlist .= '<p>THERE IS NO CATEGORIES, BEFORE YOU CAN CONTINUE YOU MUST CREATE NEW BLOG CATEGORIES!</p>';
}
within the pre tags which are within the tinymce editor it then out puts it like this:
<pre>
<br>if($getcats_num > 0){
<p>$catlist .= '<select name="bcats" id="bcats" class="selectboxit">';
<br>$catlist .= '<option value="false">Select Blog Category</option>';
<br>while($fetchcats = $getcats->fetch()){
<br>$catlist .= '<option value="'.$fetchcats['id'].'"';
<br>if(isset($_GET['mode']) && $_GET['mode'] == 'edit'){ if($fetchcats['id'] == $bcats){ $catlist .= ' selected'; } }
<br>$catlist .= '>'.$fetchcats['name'].'</option>';
<br>}
<br>$catlist .= '</select>';</p>
<p>} else {
<br>$catlist .= '<p>THERE IS NO CATEGORIES, BEFORE YOU CAN CONTINUE YOU MUST CREATE NEW BLOG CATEGORIES!</p>';
<br>}</p>
</pre>
I have tried everything to stop it adding br tags or p tags within the pre element.
I do not want to use php to simply wipe all p and br tags because then if it is used within the pre tags as a demo code then it will not show :(
Any ideas will be most grateful :)
Thanks

Trying to change statement so create_radio function is only called once (PHP)

I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price')
{
// Start the element:
echo '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
echo ' checked="checked"';
}
// Complete the element:
echo " /> $value ";
} // End of create_radio() function.
I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span>
I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!
function create_radio($value, $name = 'gallon_price')
{
$output = "";
if (is_array($value)) {
while (count($value) > 0) {
$arr_value = array_pop($value);
$output .= create_radio($arr_value);
}
} else {
// Start the element:
$output .= '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
$output .= ' checked="checked"';
}
// Complete the element:
$output .= " /> $value ";
}
return $output;
}
A quick bit of recursion will allow the function to work for arrays and non arrays. Note: in the html you will need to echo the call to create_radio not just call it.
you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){
foreach($value as $v){
// Start the element:
$out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';
// Check for stickiness:
if(isset($_POST[$name])&&($_POST[$name]==$v)){
$out .= ' checked="checked"';
}
// Complete the element:
$out .= " /> $v ";
}
return $out;
} // End of create_radio() function.
call it:
echo create_radio(array('3.00','3.50','4.00'));
it is usually better not to echo inside the function.

php trims everything after space

I'm really scratching my head...
I'm trying to add a class to my input and textarea fields, but whenever I do it like this:
$inputclass = $class." multilanginput";
the second part is not appended, but when I leave out the space and do it like this:
$inputclass = $class."multilanginput";
it works just fine...
i've never had this issue before, any ideas what's going wrong?
it's part of this little function:
function backend_dynamic_dialoginput($label,$class,$type = 'single',$lang = "none"){
if($lang == "none"){
$lang = "";
}
else{
$lang = "_".$lang;
}
$class = $class.$lang;
$id = "";
if($type == "singleint" || $type == "multiint"){
$id = $class."_m";
$inputclass = $class." multilanginput";
}else{
$inputclass = $class;
}
$html = "
<div style='padding-left:10px;margin-top:1px;background-color:#dddddd;padding-bottom:8px;padding-top:8px;'>
<div style='float:left;font-size:13px;color:#5a5a5a;padding-top:6px;margin-bottom:2px;width:30%;text-align:right;padding-right:4px;' class='font_lato'>".$label."</div>
";
if($type == "single" || $type == "singleint"){
$html .= "<input type='text' value=".$inputclass." style='font-size:12px;width:60%;border:1px solid #ffffff;padding:2px;background-color:#dddddd;' class='font_din' id='".$id."' class='".$inputclass."'>";
}
else if($type == "multi" || $type == "multiint"){
$html .= "<textarea style='font-size:12px;width:60%;border:1px solid #ffffff;padding:2px;background-color:#dddddd;' class='font_din' id='".$id."' class='".$inputclass."' rows=2></textarea>";
}
$html .= "
";
if($type == "singleint" || $type == "multiint"){
$html .= "<div style='float:right;font-size:12px;background-color:#eeeeee;margin-right:4px;' id='".$class."' class='togglefullscr font_lato'>Int.</div>";
}
$html .= "
</div>";
if($type == "singleint" || $type == "multiint"){
$html .= backend_dynamic_internationaldialog($label,$class,$type);
}
return $html;
}
You have not put quotes around the HTML attribute.
Change the line to:
$html .= "<input type='text' value='".$inputclass."' style='font-size:12px;width:60%;border:1px solid #ffffff;padding:2px;background-color:#dddddd;' class='font_din' id='".$id."' class='".$inputclass."'>";
When you omit the quotes like this, the second class name will be treated as another property.
Problems like this can be easily avoided if you validate your markup ;-)
Your quotation marks are wrong:
$html .= "<input type='text' value=".$inputclass."
It has to be $html .= "<input type='text' value='".$inputclass."' or $html .= "<input type='text' value=\"".$inputclass."\".
However, I strongly recommend using single quotes for strings:
$html .= '<input type="text" value="'.$inputclass.'"...>';.
You can escape spaces using \x20 and \040 in php.
http://php.net/manual/en/regexp.reference.escape.php
Please see the manual

Categories