On my about the artist page when you click on the words button, and the will open a modal that gets information from a DB. Right now when you click on one artist button, close the modal, and then click on another artist both artist information will pop up. What I am trying to do is make sure the information only shows one artist at a time. I'm not sure if there is an issue with my ajax, or my model.
Here is a link to my live site now
Model
<?php
class Artist_get_model extends CI_Model {
function getData() {
// $query = $this->db->get_where("artist_content", array(" " => $page));
$query = $this->db->query("SELECT * from artist_content");
return $query;
}
// FIRST LAST NAME
// MODEL CHANGES
// SELECT * from artist_content" where lastname =
function modalData($lastname) {
$query = $this->db->query("SELECT * FROM artist_content WHERE last_name = '$lastname'");
return $query;
// return $lastname;
}
}
?>
PHP Artist Page
<?php
// echo $query->num_rows();
echo '<div class="container">';
echo '<div style="margin-top: 13%;" class=" artistHeader page-header">';
echo '<h1>Meet the artist</h1>';
echo '</div>';
echo '</div>';
echo '<div class="panel panel-default profile">';
echo ' <div class="panel-body">';
echo '<div class="container-fluid">';
echo '<div class="row">';
for( $i = 0 ; $i < $query->num_rows() ; $i++ ){
$row = $query->row_array($i);
// echo $row['workLink_img2'];
// for()
// echo '<div class="row"';
echo '<div id="noPadding" class="col-md-4">';
echo '<div class="thumbnail noPadding">';
// PROFILE IMG
echo '<img src="'.$row['profile_img'].'" alt=""></img>';
echo '<div class="caption">';
// NAME
echo '<h3 class="artistName">'.$row['first_name'].' '.$row['last_name'].'</h3>';
echo '<p><a class="button btn btn-primary " id="hello" role="button" data-toggle="modal" data-target="#myModal" data-artist="'.$row['last_name'].'">Words</a>';
echo '</div>';
echo '</div>';
echo "</div>";
// echo "</div>";
// Modal
// Modal content
echo '<div id="myModal" class="modal fade" role="dialog">';
echo '<div class="modal-dialog modal-lg">';
echo '<div class="modal-content">';
echo '<div class="modal-header">';
echo '<button type="button" class="close" data-dismiss="modal">×</button>';
// GET NAME FROM DB
echo '<h4 class="modal-title">'.$row['first_name'].' '.$row['last_name'].' </h4>';
echo '</div>';
echo '<div class="modal-body">';
echo '<div class=" artistInfo containter-fluid">';
// LOAD PHP FILE
echo '</div>';
echo '</div>';
echo '<div class="modal-footer">';
echo '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
<script>
$(document).ready(function(){
$(".button").one("click", function(){
$.ajax({
type: "POST",
url: "/Artists/modal",
data: ({lastname: $(this).attr("data-artist") }),
success: function(result){
$(".artistInfo").append(result);
}});
});
});
Modal
<?php
for( $i = 0 ; $i < $artist->num_rows() ; $i++ ){
$row = $artist->row_array($i);
echo '<img class="pull-left img-responsive col-md-8" src=" '.$row['profile_img'].' ">';
echo '<div class="row">';
// SIDE IMG
echo '<a href="'.$row['workLink_img1'].'">';
echo '<img id="partsIm "class=" col-sm-4 pull-left img-thumbnail" src="'.$row['work_img1'].'"></img>';
echo '</a>';
// SIDE IMG
echo '<a href="'.$row['workLink_img2'].'">';
echo '<img id="partsIm" class="col-sm-4 pull-left img-thumbnail" src="'.$row['work_img2'].'"></img>';
echo '</a>';
// TEXT
echo '<p style="clear:both">'.$row['text'].'</p> ';
echo '</div>';
echo '</div>';
}
?>
It's in your button click event:
$(".button").one("click", function(){
$.ajax({
type: "POST",
url: "/Artists/modal",
data: ({lastname: $(this).attr("data-artist") }),
success: function(result){
//you're appending multiple data here
// $(".artistInfo").append(result);
// this instead:
$(".artistInfo").html(result);
}
});
});
});
lets empty it first since ajax can have a time lag...that way any time it takes to get new data it won't be displaying old data:
$(".button").on("click", function(){
$(".artistInfo").empty();// clear old html out
$.ajax({
type: "POST",
url: "/Artists/modal",
data: ({lastname: $(this).attr("data-artist") }),
success: function(result){
$(".artistInfo").append(result);
}});
});
append() adds to existing html and does not remove it thus you kept adding each time. If element is empty it won't matter what insertion method is used
NOTE: switched to on() instead of one() which is only intended to be used for a single event
Related
I have a custom field selectbox where I chose which Posts I want to be displayed. Now I want the query, where I get the selected Posts and its custom fields, to somehow be used as an AJAX-call so that I can use the data-output to build a masonry-grid. Is that possible?
here is my data/query:
$posts = get_field('choose_artist');
if( $posts ): ?>
<?php foreach( $posts as $p ): ?>
<div class="artist">
<a href="<?php echo get_permalink( $p->ID ); ?>">
<div style="position: relative;">
<?php $image = get_field('artist_image', $p->ID); ?>
<div class="artist-image" style="background-image:url('<?php echo $image['url']; ?>')"></div>
<div class="overlay">
<div class="content">
<p><?php the_field('artist_name', $p->ID); ?></p>
</div>
</div>
</div>
</a>
</div>
<?php endforeach; ?>
So, my goal is something like:
$.ajax({
url: '/the query above',
success: function(data){
data is the query html
}})
Is that even possible?
ajax.php
$content = '';
if( $posts ) {
foreach( $posts as $p ) {
$image = get_field('artist_image', $p->ID);
$content .= '<div class="artist">';
$content .= '<a href="' . get_permalink( $p->ID ) . '">';
$content .= '<div style="position: relative;">';
$content .= '<div class="artist-image" style="background-image:url("' . $image['url'] . '")"></div>';
$content .= '<div class="overlay">';
$content .= '<div class="content">';
$content .= '<p>' . get_the_field('artist_name', $p->ID). '</p>';
$content .= '</div>';
$content .= '</div>';
$content .= '</div>';
$content .= '</a>';
$content .= '</div>';
}
}
echo $content;
main.js / .php (whereever you have the ajax call)
$.ajax({
url: '/ajax.php', //Path to the ajax.php script
success: function(data){
console.log(data);
}});
Check the documentation for how to catch errors, check for success etc. etc: http://api.jquery.com/jquery.ajax/
There are better ways to do the AJAX page, so refactor it and make it neater, but something similar to the above should work for you if I am understanding your needs, though you'll probably need to tweak it to get what you want.
i have a tabbed box with two tabs, one that contains "characters" and one that contains "weapons". if i load the images into their respective containers using PHP directly inside my HTML, the images drag fine, but i can't get both containers to load their contents; it only loads the contents retrieved by the first instance of PHP code. when i put the respective codes into separate php files and use .load(), though, the contents load fine, but are no longer draggable.
HTML for the tab box:
<div class="tabBox">
<ul class="tabs">
<li class="card-gallery" active>Cards</li>
<li class="weapon-gallery">Weapons</li>
</ul>
<div class="tabContainer">
<div id="card-gallery" class="tabContent">
</div>
<div id="weapon-gallery" class="tabContent">
</div>
</div>
</div>
jQuery script to make the tabs work:
$(".tabContent").hide();
$('#card-gallery').load('cardgallery.php');
$('#weapon-gallery').load('weaponsgallery.php');
$(".tabContent").eq(0).show();
$(".tabs li").on('click', function(event) {
var showTab = $(this).attr('class')
if (showTab == 'card-gallery') {
$('.tabContent').hide();
$('#card-gallery').show();
$(this).siblings().removeAttr('active');
$(this).attr('active',true);
} else if (showTab == 'weapon-gallery') {
$('.tabContent').hide();
$('#weapon-gallery').show();
$(this).siblings().removeAttr('active');
$(this).attr('active',true);
}
});
jQuery draggable (currently only for "characters" tab):
$("#card-gallery .draggable").draggable({
containment: "document",
helper: "clone",
appendTo: 'body',
scroll: false
});
PHP for loading images:
<?php
$db = new mysqli("localhost", "____", "____", "builder")
or die('Error connecting to MySQL server.');
$result = mysqli_query($db, 'SELECT id, i_card_type, name, i_character,
i_rarity, i_weapon, image, special_icon
FROM card_data
LIMIT 1,200');
$i=0;
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
if (strpos($row[2], '(') !== false) {
$cardtitle = preg_split("/(【|】|\(|\))/u", $row[2]);
} else {
$cardtitle = preg_split("/(【|】)/u", $row[2]);
}
if ($i++ % 3 == 0) echo '<div class="row">';
echo '<div class="card-box">';
echo '<div id="' . $row[0] . '" class="draggable"
data-card-type="' . $row[1] . '"data-cid="' . $row[3] . '"
data-wid="' . $row[5] . '" data-rarity="' . $row[4] . '">';
echo '<div class="icon-box">';
echo '<div class="invalid-card-text"></div>';
echo '<div class="icon-image">';
echo '<img src="' . $row[6] . '" />';
if ($row[7] != 'NULL') {
echo '<img class="sicon" src="' . $row[7] . '" />';
}
echo '<img class="wicon" src="weapons/img/type/' . $row[5] . '.png" />';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<p class="card-name">';
echo '【<b>' . $cardtitle[1] . '</b>】';
echo '<br />';
echo $cardtitle[2];
if (strpos($row[2], '(') !== false) {
echo '<br />';
echo '<p class="small">( ' . $cardtitle[3] . ' )</p>';
}
echo '</p>';
echo '</div>';
if ($i % 3 == 0) echo '</div>';
}
?>
nevermind, my bad, i figured it out just a few minutes after posting this. both
$('#card-gallery').load('cardgallery.php', function() {
$('.draggable').draggable({
containment: "document",
helper: "clone",
appendTo: 'body',
scroll: false
});
});
&
function allowDrag() {
$('.draggable').draggable({
containment: "document",
helper: "clone",
appendTo: 'body',
scroll: false
});
}
$('#card-gallery').load('cardgallery.php', allowDrag)
work.
i would like to change a link to target="_blank" (opening in a new window, or tab) but can#t fix it. i´m a fool in php and my try&error-method did´t work. can anybody help me?
Thank you so much!
original code from the parallax one theme (wordpress)
<?php
if(!empty($parallax_one_contact_info_item_decoded)){
foreach($parallax_one_contact_info_item_decoded as $parallax_one_contact_item){
if(!empty($parallax_one_contact_item->link)){
echo '<div class="col-sm-4 contact-link-box col-xs-12">';
if(!empty($parallax_one_contact_item->icon_value)){
echo '<div class="icon-container"><span class="'.esc_attr($parallax_one_contact_item->icon_value).' colored-text"></span></div>';
}
if(!empty($parallax_one_contact_item->text)){
echo ''.$parallax_one_contact_item->text.' ';
}
echo '</div>';
} else {
echo '<div class="col-sm-4 contact-link-box col-xs-12">';
if(!empty($parallax_one_contact_item->icon_value)){
echo '<div class="icon-container"><span class="'.esc_attr($parallax_one_contact_item->icon_value).' colored-text"></span></div>';
}
if(!empty($parallax_one_contact_item->text)){
if(function_exists('icl_translate')){
echo ''.icl_translate('Contact',$parallax_one_contact_item->id.'_contact',esc_attr($parallax_one_contact_item->text)).'';
} else {
echo ''.esc_attr($parallax_one_contact_item->text).'';
}
}
echo '</div>';
}
}
}
?>
Simply add target="_blank" in your anchor tag. For example :
echo '<a target="_blank" href="'.$parallax_one_contact_item->link.'" class="strong">'.$parallax_one_contact_item->text.' </a>';
Try this:
<?php
if(!empty($parallax_one_contact_info_item_decoded)){
foreach($parallax_one_contact_info_item_decoded as $parallax_one_contact_item){
if(!empty($parallax_one_contact_item->link)){
echo '<div class="col-sm-4 contact-link-box col-xs-12">';
if(!empty($parallax_one_contact_item->icon_value)){
echo '<div class="icon-container"><span class="'.esc_attr($parallax_one_contact_item->icon_value).' colored-text"></span></div>';
}
if(!empty($parallax_one_contact_item->text)){
echo '<a target="_blank" href="'.$parallax_one_contact_item->link.'" class="strong">'.$parallax_one_contact_item->text.' </a>';
}
echo '</div>';
} else {
echo '<div class="col-sm-4 contact-link-box col-xs-12">';
if(!empty($parallax_one_contact_item->icon_value)){
echo '<div class="icon-container"><span class="'.esc_attr($parallax_one_contact_item->icon_value).' colored-text"></span></div>';
}
if(!empty($parallax_one_contact_item->text)){
if(function_exists('icl_translate')){
echo '<a target="_blank" href="" class="strong">'.icl_translate('Contact',$parallax_one_contact_item->id.'_contact',esc_attr($parallax_one_contact_item->text)).'</a>';
} else {
echo '<a target="_blank" href="" class="strong">'.esc_attr($parallax_one_contact_item->text).'</a>';
}
}
echo '</div>';
}
}
}
?>
Note: Added target="_blank" in each hyperlink.
I use bootstrap with JQuery and I would like to use tabs to navigate page by page.
Indeed, I have some results in a table. With SQL, I know how to do this by using LIMITE 1,10 for example. And by using a $_GET argument to navigate page by page...
But here, I would like use tabs instead.
Here is my code:
echo '<div class="tab-content">';
echo '<div class="tab-pane fade in active" id=1>';
while($row = $selectAllUsersM->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Desable' : 'Enable';
echo "<tr>"; echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo '</div>';
echo '</div>';
Here is the example for pagination:
<ul class="pagination">
<li>«</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>»</li>
</ul>
I don't know how can I display 10 results on each page. Then, if 11 results, create a new page and a new tab. I hope you know what I mean.
Thank you for your help ;)
Well, I solved this by using a POST in Ajax.
Function to display results
<script>
function displayPilotesF(pageF) {
$.post("pilotesF.php",
{ page : pageF},
function(data){
//alert("Data Loaded: " + data);
$("#pilotesF").html(data);
}
);
}
</script>
I display the results in a div:
<div id="pilotesF"></div>
And on my page "pilotesF.php":
if(isset($_POST['page'])){
$page = $_POST['page'];
$selectAllUsersF= getAllInfoOfAllUsersF($page, nbPiloteFPerPage);
$selectAllUsersF->execute();
echo "<table class='table table-hover table-condensed'>";
echo "<th>Firstname</th>";
echo "<th>Name</th>";
echo "<th>Nickname</th>";
echo "<th>Email</th>";
echo "<th>Registration</th>";
echo "<th>State account</th>";
while($row = $selectAllUsersF->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Disable' : 'Enable';
echo "<tr>";
echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo "</table>";
}
And for my pagination:
$nbPages= ceil($nbPiloteF/ nbPiloteFPerPage);
echo '<div class="text-center">';
echo '<ul class="pagination" id="tabs2">';
/***********************************/
for ($i = 1 ; $i <= $nbPages; $i++){
if($i == 1)
echo '<li class="active"><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
else
echo '<li><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
}
/***********************************/
echo '</ul>';
echo '</div>';
Maybe it could help some of you guys,
Thank you anyway, have a night! :)
I have a page that features certain image thumbs from a custom field. What I need to do is have the link on the images thumbs link directly to it's image in the slider, the problem is that the slider is in other page not the same.
This is the code i'm using to get the images from the custom post repeater field:
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $slides = get_field('project_slider');
// Grabs the array
if($slides) {
foreach($slides as $s) {
echo '<div id="project_slider" class="item"> ';
echo '<div class="aimagediv" >'; //
echo '<a>';
echo '<img src="'.$s['project_image'].'" alt="" width="240" />';
echo '</a>';
echo '</div>'; //aimagediv
echo '<div class="art_title">';
echo '<p>SWEET LIFE</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //first div
}
}
?>
<?php endwhile; // end of the loop. ?>
I'm using this to link to the slider page ( I know it's wrong link that way but I had to do it to show you how it links to slider):
$('#project_slider').click(function() {
location.href = '?page_id=42';
});
and this is the code of the jquery cycle plugin i'm using for the slider:
$("#slideshow").css("overflow", "hidden");
$('ul#slides').cycle({
fx: 'fade',
timeout: 0,
prev: '#prev',
next: '#next',
after: onAfter
});
function onAfter(curr,next,opts) {
var caption = '' + (opts.currSlide + 1) + ' / ' + opts.slideCount;
$('.project_number p').html(caption);
}
How can I fix to link each project to it's corresponding position in the slider, instead of always opening in the start image of the slider?
here it's a working example of what i'm trying to achieve: loscarpinteros.net/#exhibitions any image get's you to the link of it in the slider position.
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $slides = get_field('project_slider');
// Grabs the array
if($slides) {
$i = 0;
foreach($slides as $s) {
echo '<div id="project_slider" counter="'.$i++.'" class="item"> ';
echo '<div class="aimagediv" >'; //
echo '<a>';
echo '<img src="'.$s['project_image'].'" alt="" width="240" />';
echo '</a>';
echo '</div>'; //aimagediv
echo '<div class="art_title">';
echo '<p>SWEET LIFE</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //first div
}
}
?>
<?php endwhile; // end of the loop. ?>
in JS
$('#project_slider').click(function() {
location.href = '?page_id=42&slide='+$(this).attr('counter');
});
and in JQuery cycle
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
$("#slideshow").css("overflow", "hidden");
$('ul#slides').cycle({
fx: 'fade',
timeout: 0,
prev: '#prev',
next: '#next',
after: onAfter,
startingSlide: getURLParameter('slide'),
});
function onAfter(curr,next,opts) {
var caption = '' + (opts.currSlide + 1) + ' / ' + opts.slideCount;
$('.project_number p').html(caption);
}
BTW you can make href of a tag in your php loop.