I have this loop which outputs data:
foreach ($userItems_get as $item => $value) {
if ($value['prefab'] == 'wearable') {
echo $value['name'] . "</br>";
echo "<img src=\"{$value['image_inventory']}.png\" width=\"90\" height=\"60\">" . "</br>";
if (!isset($value['item_rarity'])) {
$rarity = "common";
} else {
$rarity = $value['item_rarity'];
}
echo $rarity . "</br>";
foreach ($userItemsLoad as $key => $values) {
if ($item == $values['defindex']) {
echo $values['id'] . "</br></br>";
break;
}
}
}
}
I want it to display like this: http://puu.sh/l7gyH/afe812c4f3.jpg
for which i have this html:
<div class="item-container">
<div id="item-box">
<img src="http://lorempixel.com/output/technics-q-c-95-60-9.jpg" alt="">
<div id="rarity">
rare
</div>
</div>
Which ever items the user selects i want to gets it values['id'], the user maybe select multiple items at once.
I can create a normal checkpoint type input table but the user might have 1000's of items so it needs to this way like in the picture.
Is there a simple way to accomplish this? Thanks
<div class="item-container">
<div class="item-box" data-id="1234">
<img src="http://lorempixel.com/output/technics-q-c-95-60-9.jpg" alt="">
<div id="rarity">
rare
</div>
</div>
</div>
<script>
var selectedItems = [];
$('.item-box').on('click', function(e){
$(this).toggleClass('selected');
var selectedItems = []
$('.item-box.selected').each(function(i,o){
selectedItems.push($(this).data('id'))
})
});
</script>
Personally thought I would think about using AngularJS (https://angularjs.org/) for something like this, it will make your life a lot easier.
Related
I'm building a website using PHP and AJAX JQUERY. And inside this website, there is texting system, like whatsapp, telegram ...etc.
and I'm wanna get all the messages from the database after the user send a message, using jQuery AJAX. so far I have been able to send the text to the database successfully but I fail when I try to get the messages.
I Have this following code when first the page is loaded:
<div class="messaging-frame">
<div class="Title-in-message">
<h4 class="title-message"><?php echo $index['Title'] ?></h4>
</div>
<div class="messaging-box" id="messages">
<?php
// THE MESSAGE FROM MESSAGE TABLE
foreach ($messages as $key => $value) {
if ($_SESSION['EmployeeNum'] === $value['Sender']) { ?>
<div class="row">
<div class="col" style="margin-bottom:15px">
<div class='sent'>
<p id='send-bubble'> <?php
echo $value['Message'] ?> </p>
</div>
</div>
</div>
<?php
} else { ?>
<div class="row">
<div class="col" style="margin-bottom:15px">
<div class='recived' id="recieved-bubble">
<p id='recived-bubble'><?php echo $value['Message'] ?></p>
</div>
</div>
</div><?php
}
}
?>
</div>\
Than this code in AJAX jQuery get executed when ever the user send a new message in messaging box:
$(document).ready(function() {
$('#sending_text').click(function() {
var report = $('#report_num').val()
var reportInt = parseInt(report);
var text = $('#text_sent').val();
var encodeText = encodeURIComponent(text);
$.ajax({
url: "<?php echo base_url() . 'DisplayTicket/sendNewText' ?> ",
type: "POST",
data: {
text: text,
reportNum: reportInt
},
success: function() {
$('#messages').load("<?php echo base_url() . 'DisplayTicket/getNewMessages' ?>", {reportNum: reportInt}, function() {
var texts = " <?php foreach ($messages as $k => $val) { if ($_SESSION['EmployeeNum'] === $value['Sender']) { ?>";
texts += " <div class='row'> <div class='col' style='margin-bottom:15px'> <div class='sent'>";
texts += "<?php echo $val['Message'] ?>";
texts += "</div> </div> </div>";
texts += "<?php } else { ?>";
texts += " <div class='row'> <div class='col' style='margin-bottom:15px'> <div class='recived' id='recieved-bubble'>"
texts += "<?php echo $val['Message'] ?>";
texts += "</div> </div> </div>"
texts += "<?php }
} ?>";
texts += "</div>";
$('#messages').html(texts);
})
},
});
});
})
Keep in mind that the $('#messages').html(texts);
is actually get executed but the newest messages in not added
I see you are using .load() method to fill the #messages .
Normally the first argument to this method is the response you wanna insert,and the this is just a callback after success, and you are using a php variable $messages instead of using the response from the request.
I see already 2 ways of doing this:
1 - remove the callback from load() and make sure the response is well written in html as youy want
2 - get rid of load() function, and call another ajax inside success of the first ajax to accomplish so.
The second method is just to make you understand what is wrong. So the best way to do it is to fix load() method accordingly
In my page there is a div element containing a MetroUI listview :
...
<div id="listSalles" class="listview">
<?php
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ( $ret as $key => $val ) {
$html .= '<div class="list" id="salle_'.$ret[$key]->salle_code.'_'.$ret[$key]->flag_reserver.'">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">'.$ret[$key]->salle_lib.'</span>
<span class="place-right"><button id="reservS_'.$ret[$key]->salle_code.'" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">'.$ret[$key]->reserver.'</span>
</div>';
}
echo $html;
}
else {
echo '<br/><div class="sub-header">Aucun enregistrement</div>';
}
?>
</div>
...
As you can see there is a database SELECT query which populates the listview. I want to "reload" this listview's content when a button , outside of the listview , is pressed. How to do that ?
Use javascript to do so.
In your PHP script put your current code:
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ($ret as $key => $val) {
$html .= '<div class="list" id="salle_' . $ret[$key]->salle_code . '_' . $ret[$key]->flag_reserver . '">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">' . $ret[$key]->salle_lib . '</span>
<span class="place-right"><button id="reservS_' . $ret[$key]->salle_code . '" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">' . $ret[$key]->reserver . '</span>
</div>';
}
return $html;
} else {
return '<br/><div class="sub-header">Aucun enregistrement</div>';
}
Lets call it data.php.
Then in your page load jQuery (if you havent yet, because it's easier) and add the following JS:
$.ajax({
type: 'GET',
url: 'data.php'
}).done(function (result) {
$("#listSalles").html(data); //Here you replace the current content with the update
}, "html");
And then wrap this JS in $(document).ready(function() {}) and inside a click listener $(btn).click(function(){ });
I suggest using AJAX.
Make a view with the relevant php code for the task, like this:
<?php
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ( $ret as $key => $val ) {
$html .= '<div class="list" id="salle_'.$ret[$key]->salle_code.'_'.$ret[$key]->flag_reserver.'">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">'.$ret[$key]->salle_lib.'</span>
<span class="place-right"><button id="reservS_'.$ret[$key]->salle_code.'" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">'.$ret[$key]->reserver.'</span>
</div>';
}
echo $html;
}
else {
echo '<br/><div class="sub-header">Aucun enregistrement</div>';
}
?>
Let's call it list-view.php.
Then in your page, add the following jquery:
$("#listSalles").load("list-view.php");
That would load the view inside that div.
If you want to reload that content when you press a button you could add a click handler for that button:
$("#id_of_button").click(function(){ $("#listSalles").load("list-view.php"); });
document.friendform1.friends[i].checked is not working when there is only one element in checkbox array. If there are more than one element in checkbox array than it is working fine
my html and php code is
$x=0;
foreach($result as $row)
{
if($x==0)
{?>
<div class="invite-friends-con">
<?php
}else{?>
<div class="invite-friends-con" style="margin-left:70px;">
<?php
}?>
<div class="invite-friends-con-img">
<img src="<?php echo base_url();?>Profile_images/<?php echo $row->vchPicture?>" width="48" height="39" align="absmiddle" />
</div>
<span><?php echo $row->vchFirstName?></span><input type="checkbox" name="friends[]" style="margin-top:10px" value="<?php if($row->intFriendID==$this->session->userdata('user_id')){ echo $row->intMemberID;}else{ echo $row->intFriendID;}?>" id="friends" />
<input type="hidden" name="frnd[]" id="frnd" value="<?php echo $row->vchFirstName?>" />
<?php if($x==1){$x=0;}else{$x++;}?>
<?php
} ?>
and javascript code is:
function addalfriend()
{
var str='';
var str1='';
var len=document.friendform1.friends.length;
for(i=0;i<len;i++)
{
if(document.friendform1.friends[i].checked==true)
{
if(str=='')
{
str=document.friendform1.friends[i].value;
str1=document.friendform1.frnd[i].value;
}
else
{
str=str+","+document.friendform1.friends[i].value;
str1=str1+","+document.friendform1.frnd[i].value;
}
}
}
document.getElementById('invite_1').value=str;
if(str!='' && str1!=''){
document.getElementById('invited').style.display="block";
}
else{
document.getElementById('invited').style.display="none";
}
document.getElementById('invited').value = str1;
document.getElementById("low-high-div").style.display="none";
document.getElementById("events-input-bg2").value="";
document.getElementById("events-input-bg3").value="";
closemessage1();
return false;
}
So please someone let me know my mistake.
Thanks
when there is one item return then array is not retuned element object is returned so you need to access it differently then
as
var len=document.friendform1.friends.length;
if(len>1)
for(i=0;i<len;i++)
{
if(document.friendform1.friends[i].checked==true)
{
if(str=='')
{
str=document.friendform1.friends[i].value;
str1=document.friendform1.frnd[i].value;
}
.
.
.
}
else
{
if(document.friendform1.friends.checked==true)
{
if(str=='')
{
str=document.friendform1.friends.value;
str1=document.friendform1.frnd.value;
}
}
.
.
.
}
You can try it simply like:
var len=document.friendform1['friends[]'].length || 1;
for(i=0;i<len;i++)
{
...
...
...
}
...
...
...
Read Why I can not get checkbox length while using it differently?
I'm trying to make simple cms; the code below is index.php.
Now I can't find the way to make jquery select each item, because they have the same name.
For example i want to do is: select (on $(.subject)).hover then (a $(.subjectpic)).show
How to make them have their own names?
<?php
if(!$query){
print "submit error";
}
else {
while($list=mysql_fetch_array($query)){
print"
<div class=\"subjectwrap\">
<div class=\"subject\">$list[subject]</div>
<div class=\"subjectpic\">$list[subjectpic]</div>
</div>
";
}
}
?>
There are two options. You can either do it all with jQuery, selecting the sibling subjectpic:
$('.subject').hover( function() {
$(this).closest('.subjectwrap').find('.subjectpic').show();
});
Or you can do it with PHP, assigning each a numerical data-subject index:
<?php
if(!$query){
print "submit error";
}
else {
$i = 1;
while($list=mysql_fetch_array($query)){
print"
<div class=\"subjectwrap\">
<div class=\"subject\" data-subject=\"$i\">$list[subject]</div>
<div class=\"subjectpic\" data-subject=\"$i\">$list[subjectpic]</div>
</div>
";
$i++;
}
}
?>
By assigning a numerical index in PHP, you could then target it with jQuery:
$('.subject').hover( function() {
var thisSubject = $(this).attr('data-subject');
$('.subjectpic[data-subject=' + thisSubject + ']').show();
});
I just asked another question here: global variable and reduce database accesses in PHP+MySQL
I am using PHP+MySQL. The page accesses to the database and retrieve all the item data, and list them. I was planning to open a new page, but now I want to show a pop div using javascript instead. But I have no idea how to utilize the variables of PHP in the new div. Here is the code:
<html>
</head>
<script type="text/javascript">
function showDiv() {
document.getElementById('infoDiv').style.visibility='visible';
}
function closeDiv() {
document.getElementById('infoDiv').style.visibility='hidden';
}
</script>
</head>
<body>
<ul>
<?php foreach ($iteminfos as $iteminfo): ?>
<li><?php echo($iteminfo['c1']); ?></li>
<?php endforeach;?>
</ul>
<div id="infoDiv" style="visibility: hidden;">
<h1><?php echo($c1) ?></h1>
<p><?php echo($c2) ?></p>
<p>Return</p>
</div>
</body>
</html>
"iteminfos" is the results from database, each $iteminfo has two value $c1 and $c2. In "infoDiv", I want to show the details of the selected item. How to do that?
Thanks for the help!
A further question: if I want to use, for example, $c1 as text, $c2 as img scr, $c1 also as img alt; or $c2 as a href scr, how to do that?
Try this:
<?php foreach ($iteminfos as $iteminfo): ?>
<li>
<a href="javascript:showDiv(<?php echo(json_encode($iteminfo)) ?>)">
<?php echo($iteminfo['c1']); ?>
</a>
</li>
<?php endforeach;?>
Also, modify showDiv to take your row data:
function showDiv(row) {
document.getElementById('infoDiv').style.visibility='visible';
document.getElementById('infoDiv').innerHTML = row['c1'];
}
Basically, you have to consider that the javascript runs in the browser long after the PHP scripts execution ended. Therefore, you have to embed all the data your javascript might need into the website or fetch it at runtime (which would make things slower and more complicated in this case).
Do you want a single info area with multiple items listed on the page and when you click an item the info area is replaced with the new content??? or you want a new info area for each item??
I see something along the lines of the first approach, so I will tackle the latter.
<?php
//do some php magic and get your results
$sql = 'SELECT title, c1, c2 FROM items';
$res = mysql_query($sql);
$html = '';
while($row = mysql_fetch_assoc($res)) {
$html .= '<li><a class="toggleMe" href="#">' . $row['title'] . '</a><ul>';
$html .= '<li>' . $row['c1'] . '</li><li>' . $row['c2'] . '</li></ul>';
$html .= '</li>'; //i like it when line lengths match up with eachother
}
?>
<html>
</head>
<script type="text/javascript">
window.onload = function(){
var els = document.getElementsByClassName("toggleMe");
for(var i = 0, l = els.length; i < l; i++) {
els[i].onclick = function() {
if(this.style.display != 'none') {
this.style.display = 'block';
} else {
this.style.display = 'none';
}
}
}
}
</script>
</head>
<body>
<ul>
<?php
echo $html;
?>
</ul>
</body>
</html>