Why does this block my PHP code? - php

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.

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();
}

echo works twice just for one id but doesn't for the other one

There are 2 entries on the table. But it echos 3 entries but it echos twice one of them. If i change it to ASC from DESC, then it echos the other one twice. If i use "where id <> 5" then it just echos id 6 just once. But it is a dynamic site so... And i use the same exact code on another page and it works. Here is the full code:
<?php
$cek = mysql_query('select id,isim,aciklama,tarih from galeri where dil = '.$dbDil.' order by id desc');
while($kaynak = mysql_fetch_assoc($cek)){
$cekG = mysql_query('select resim_url from galeriresim where galeriID = '.$kaynak['id'].' order by id desc');
$galeri .= '<h1 class="sayfaBaslik fl"><span>'.$kaynak['tarih'].'</span> '.$kaynak['isim'].'</h1>';
$galeri .= '<h2 class="sayfaAciklama fl">'.$kaynak['aciklama'].'</h2>';
$galeri .= '<div class="sayfaIcerik" style="width:100%">';
$galeri .= '<div class="galeriH fl swiper-container-'.$kaynak['id'].'">';
$galeri .= '<ul class="fl swiper-wrapper-'.$kaynak['id'].'">';
while($kaynakG = mysql_fetch_assoc($cekG)){
$galeri .= '<li class="swiper-slide-'.$kaynak['id'].'"><img src="'.$yol.'images/galeri/'.$kaynak['id'].'/'.$kaynakG['resim_url'].'" /></li>';
}
$galeri .= '</ul></div></div>';
$galeri .='<script>';
$galeri .= 'var mySwiper = new Swiper(\'.swiper-container-'.$kaynak['id'].'\',{';
$galeri .= 'moveStartThreshold : 75,';
$galeri .= 'wrapperClass : "swiper-wrapper-'.$kaynak['id'].'",';
$galeri .= 'slideClass : "swiper-slide-'.$kaynak['id'].'"';
$galeri .= '});';
$galeri .= '</script>';
echo $galeri;
}
?>
You're doing your final echo INSIDE the main while() loop:
while(..) {
while(..) { .. }
echo ..
}
It should be
while(..) {
while(..) { .. }
}
echo ..
Since you're echoing INSIDE the main loop, you'll be running that echo multiple times, spitting out $galeri as it's being built.
Try this:
i have added mysq_free_result and echo must be outside the while !
<?php
$cek = mysql_query('select id,isim,aciklama,tarih from galeri where dil = '.$dbDil.' order by id desc');
while($kaynak = mysql_fetch_assoc($cek)){
$cekG = mysql_query('select resim_url from galeriresim where galeriID = '.$kaynak['id'].' order by id desc');
$galeri .= '<h1 class="sayfaBaslik fl"><span>'.$kaynak['tarih'].'</span> '.$kaynak['isim'].'</h1>';
$galeri .= '<h2 class="sayfaAciklama fl">'.$kaynak['aciklama'].'</h2>';
$galeri .= '<div class="sayfaIcerik" style="width:100%">';
$galeri .= '<div class="galeriH fl swiper-container-'.$kaynak['id'].'">';
$galeri .= '<ul class="fl swiper-wrapper-'.$kaynak['id'].'">';
while($kaynakG = mysql_fetch_assoc($cekG)){
$galeri .= '<li class="swiper-slide-'.$kaynak['id'].'"><img src="'.$yol.'images/galeri/'.$kaynak['id'].'/'.$kaynakG['resim_url'].'" /></li>';
}
mysql_free_result($cekG);
$cekG ="";
$galeri .= '</ul></div></div>';
$galeri .='<script>';
$galeri .= 'var mySwiper = new Swiper(\'.swiper-container-'.$kaynak['id'].'\',{';
$galeri .= 'moveStartThreshold : 75,';
$galeri .= 'wrapperClass : "swiper-wrapper-'.$kaynak['id'].'",';
$galeri .= 'slideClass : "swiper-slide-'.$kaynak['id'].'"';
$galeri .= '});';
$galeri .= '</script>';
}
echo $galeri;
?>
Use a JOINed query then you wouldn't need a double while loop. you shouldnt be using mysql anyway. Use mysqli or pdo
'select g.id,g.isim,g.aciklama,g.tarih, gr.resim_url
from galeri g
JOIN galeriresim gr on gr.galeriID = g.id
where g.dil = '.$dbDil.' order by id desc'

jQuery does not apply code to content outputted by PHP

I am really stuck on an issue. I have some PHP code that outputs correctly to HTML and displays as it should in the browser (and firebug shows the source is correct), but because the information is sent by PHP to display a list of clients, my jQuery on click 'Options' menu will not work - it gets ignored. If the rows are written in flat HTML, the jQuery on click works for the 'Options' menu, so I know the jQuery is fine. On click of 'Options', the 'action-menu' ul list should toggle its display and go from none to block.
PHP:
<?php
include('dbConfig.php');
$term = $_POST['searchit'];
if($term == ''){
echo '<div class="row"><div class="col-2"></div>Enter a search term</div>';
} else {
if($client = $db->prepare("SELECT client_id, client_unique_id, client_first_name, client_last_name, client_organisation_name, client_telephone, client_list_all FROM clients WHERE client_unique_id LIKE ? OR client_first_name LIKE ? OR client_last_name LIKE ? OR client_organisation_name LIKE ? OR client_address_line_1 LIKE ? OR client_list_all LIKE ? ORDER BY client_id DESC")){
$client->bind_param('ssssss', $term, $term, $term, $term, $term, $term);
$client->execute();
$client->bind_result($client_id, $client_unique_id, $client_first_name, $client_last_name, $client_business, $client_telephone, $client_list_all);
$client->store_result();
$string = '';
if($client->num_rows > 0){
while($client->fetch()){
$properties = $db->prepare("SELECT property_id FROM properties WHERE property_client_id = ? LIMIT 1");
$properties->bind_param('i', $client_id);
$properties->execute();
$properties->store_result();
$properties->bind_result($property_id);
if($properties->num_rows > 0 ){
$active = '<span class="label label-success">ACTIVE</span>';
} else {
$active = '<span class="label label-warning">INACTIVE</span>';
}
if(!$client_business){
$client_business = 'N/A';
}
$properties->close();
$string .= '<div class="row">';
$string .= '<div class="col-2">'.$client_unique_id.'</div>';
$string .= '<div class="col-3">'.$client_first_name.' '.$client_last_name.'</div>';
$string .= '<div class="col-3">'.$client_business.'</div>';
$string .= '<div class="col-2">'.$client_telephone.'</div>';
$string .= '<div class="col-2 align-center">';
$string .= '<div class="action">Options<span class="action-arrow"><i class="fa fa-caret-down"></i></span>';
$string .= '<ul class="action-menu">';
$string .= '<li><i class="fa fa-folder-open-o"></i>View client</li>';
$string .= '<li><i class="fa fa-pencil-square-o"></i>Edit client</li>';
$string .= '<li><a onclick="deleteItem($(this))" style="cursor: pointer;" data-client-id="'.$client_id.'"><i class="fa fa-trash-o"></i>Delete client</a></li>';
$string .= '</ul>';
$string .= '</div>';
$string .= '</div>';
$string .= '</div>';
}
$client->close();
} else {
$string = '<div class="row"><div class="col-2"></div>No record found</div>';
}
echo $string;
} else {
echo '<div class="row"><div class="col-2"></div>ERROR: Could not prepare SELECT Client SQL statement.</div>';
}
}
?>
And the jQuery:
$(document).ready(function () {
$('.action').on('click', function () {
$('.action-menu').fadeToggle(200);
$(this).toggleClass('action-on');
});
});
Try changing your jQuery like this:
$(document).ready(function () {
$('div.row').on('click', '.action', function () {
$('.action-menu').fadeToggle(200);
$(this).toggleClass('action-on');
});
});
This will target the .action class elements no matter when they are loaded. Adding the selector parameter into the .on() method lets jQuery find elements that match no matter when they are created. If you have .action elements outside of the .row element, select for an element that contains them all. If all else fails, use body.
If you are adding the HTML dynamically, you can add the onclick event to any reliable parent that would exist on DOM load (eg:body).
Try to change the onclick like below,
$('body').on('click', '.action', function ()
{
$('.action-menu').fadeToggle(200);
$(e.target).toggleClass('action-on');
});
Thanks for all the support. The jQuery code that required this to work needed to be placed in the actual HTML file at the end of the PHP script - for reasons unknown, as it would not listen to the same code in the javascript.js file

dynamically changing CSS background-image

I'm fairly new to both PHP and Javascript, so please forgive my ignorance and poor use of terminology, but I'll do my best to explain exactly what I'm struggling to achieve.
I have information stored in a PHP array that I call to my index page using the function below (the code below is in a separate PHP file called articles.php that's included in my index.php) :
<?php
function get_news_feed($article_id, $article) {
$output = "";
$output .= '<article class="img-wrapper">';
$output .= '<a href="article.php?id=' . $article_id . '">';
$output .= '<div class="news-heading">';
$output .= "<h1>";
$output .= $article["title"];
$output .= "</h1>";
$output .= "<p>";
$output .= "Read more...";
$output .= "</p>";
$output .= "</div>";
$output .= '<div id="news-img-1">';
$output .= "</div>";
$output .= "</a>";
$output .= "</article>";
return $output;
}
$articles = array();
$articles[] = array(
"title" => "Andy at NABA",
"description" => "Docendi, est quot probo erroribus id.",
"img" => "img/gym-01.jpg",
"date" => "05/04/2013"
);
$articles[] = array(
"title" => "Grand Opening",
"description" => "Docendi, est quot probo erroribus id.",
"img" => "img/gym-01.jpg",
"date" => "05/04/2013"
);
?>
My index.php looks like the following minus some HTML that plays no role in this process:
<?php
include("inc/articles.php");
?>
<?php
$pageTitle = "Home";
include("inc/header.php");
?>
<section class="col-4 news">
<?php
$total_articles = count($articles);
$position = 0;
$news_feed = "";
foreach($articles as $article_id => $article) {
$position = $position + 1;
if ($total_articles - $position < 2) {
$news_feed .= get_news_feed($article_id, $article);
}
}
echo $news_feed;
?>
</section>
I am aiming to dynamically change the CSS Background-Image property of the div element with ID news-img-1 using Javascript.
I have tried such things as:
document.getElementById('news-img-1').style.backgroundImage = 'url('<?php $article["img"]; ?>')';
document.getElementById('news-img-1').style.backgroundImage = 'url('http://www.universalphysique.co.uk/' + '<?php $article["img"]; ?>')';
document.getElementById('news-img-1').style.backgroundImage = 'url('window.location.protocol + "//" + window.location.host + "/" + '<?php $article["img"]; ?>')';
.....but I'm getting nowhere!! My code in practise works because the following Javascript inserts an image correctly:
document.getElementById('news-img-1').style.backgroundImage = 'url("img/gym-01.jpg")';
Here is my site up and running, the images should be placed in the empty circles you'll see! Any help would be great, this ones tough for me!!
comparing the hard coded javascript to ones that don't work, I notice that you are not including the double-quotes around the <?php $article["img"]; ?> snippet. The hard coded one shows
= 'url("img/gym-01.jpg")'
but the ones with the php snippet will produce
= 'url(img/gym-01.jpg)'
so perhaps if you modify it to
document.getElementById('news-img-1').style.backgroundImage = 'url("'<?php $article["img"]; ?>'")';
OR
edit the get_news_feed function as follows:
replace these lines
$output .= '<div id="news-img-1">';
$output .= "</div>";
with
$output .= '<div class="news-img"><img src="' . $article["img"] . '"></div>' ;
and change your css like so:
article.img-wrapper {
position: relative;
}
div.news-img {
position: absolute;
top: 0;
z-index: -1000;
}
OR
Modify your get_news_feed function, change the statement for the <div id="news-img-1"> output to include a data-url attribute like:
$output .= '<div class="news-img" data-url="' . $article["img"] . '">';
Then add a jquery statement like:
$(".news-img").each( function() {
$(this).css("background-image", "url(" + $(this).data("url") +")" );
});
The jquery statement goes in a static js file as opposed to generating the script in php.
You need to remove the quotes from your PHP tags and see if it works!
Do it like this:
document.getElementById('news-img-1').style.backgroundImage = 'url(' + <?php $article["img"]; ?> + ')';
Hope it helps.

facebook sdk user friends image not being returned if using image as background in div

I am trying to get the profile image of a user's friend who logged in. The problem is when I use img src to display the returned image, it works and the image is displayed, but when I use div background-image then it doesn't return the correct image link.
function displayUsersIcons($criteria) {
$users = $criteria['users'];
$nb_display = $criteria['nb_display'];
$width = $criteria['width'];
if($width=='') $width="300";
if($nb_display>count($users) || $nb_display=='') $nb_display=count($users); //display value never bigger than nb users
$display = '';
for($i=0;$i<$nb_display;$i++) {
$name = $users[$i]['name'];
$picture = $users[$i]['picture'];
$url = $users[$i]['url'];
$display .= '<li class="span4">';
$display .= '<div class="thumbnail">';
$display .= '<h3>'.$name.'</h3>';
$display .= '<a href="'.$url.'" target="_blank" title="'.$name.'">';
$display .= '<div class="miprofile-pic-cnt" style="background-image:url('.$picture.')" ></div>';
$display .= '</a>';
$display .= '<div class="caption">';
$display .= '<p>Action Action</p>';
$display .= '</div>';
$display .= '</div>';
$display .= '</li>';
}
return $display;
}
In this the <div class="miprofile-pic-cnt" style="background-image:url('.$picture.')" ></div> is the div I am referring to, and in this the method of background image just displys the image links as https://graph.facebook.com/7460374244/picture?type=large, which should be something like sdsd.jpg. When I use img src intead of div, it works fine

Categories