Generate JQuery code inside a PHP foreach loop - php

UPDATE
for JLRiche
html structure below (here is the structure for the entire div id=content_body_right):
<div id="content_body_right">
<p class="user_text">Tim Flanagan</p><p class="date_text">02-06-2013 # 12:00PM</p>
<p class="message_text">Playin Augusta today. What a beautiful course!</p>
<div id="activity_image">
<img src="images/activities/1/actimg.jpg" width="435" />
</div>
<div id="tips">
<div id="tip_cap_left">
<a href="dashboard.php?captip=tipyourcap" title="Tip Your Cap" ></a>
</div>
<div id="tip_cap_right">
<p class="tips_right">12 Tips of the Cap</p>
</div>
</div>
<p class="comments_label">
4 Comments
see all
collapse
</p>
<div id="comment1">
<div id="comment_user_img">
<img src="images/defaultuserimg.jpg" width="30" height="30" />
</div>
<div id="comment_user">
<p class="user_text_comment">Tim Flanagan</p><p class="date_text_comment">02-06-2013 # 12:00PM</p>
<p class="message_text_comment">Nice jealous of you bro.</p>
</div>
</div>
<div class="comment2" style="display:none; clear:both; margin:0px; overflow:auto">
<div id="comment2_sub">
<div id="comment_user_img">
<img src="images/defaultuserimg.jpg" width="30" height="30" />
</div>
<div id="comment_user">
<p class="user_text_comment">Tim Flanagan</p><p class="date_text_comment">02-06-2013 # 12:00PM</p>
<p class="message_text_comment">Nice jealous of you bro.</p>
</div>
</div>
<div id="comment2_sub">
<div id="comment_user_img">
<img src="images/defaultuserimg.jpg" width="30" height="30" />
</div>
<div id="comment_user">
<p class="user_text_comment">Tim Flanagan</p><p class="date_text_comment">02-06-2013 # 12:00PM</p>
<p class="message_text_comment">Nice jealous of you bro.</p>
</div>
</div>
<div id="comment2_sub">
<div id="comment_user_img">
<img src="images/defaultuserimg.jpg" width="30" height="30" />
</div>
<div id="comment_user">
<p class="user_text_comment">Tim Flanagan</p><p class="date_text_comment">02-06-2013 # 12:00PM</p>
<p class="message_text_comment">Nice jealous of you bro.</p>
</div>
</div>
</div>
</div>
Let me know if you need more structure, because there is alot above and below it. Hope this helps.
I really appreciate your help!
END UPDATE
Good Evening All,
I need to dynamically create X amount of jquery scripts foreach of the X amount of db results. X meaning the the number will vary. It is sort of a forum type thing where you see the original post with one reply showing and you would click see all or collapse all to view or collapse the rest. I am incrementing my html elements inside a foreach loop with the typical $i variable so I need to output jquery click functions for each as well.
The code I need PHP to create is below:
$jquery .= "$('#see_all$i').click(function () {
$('#comment2_$i').slideDown('fast');
$('#collapse$i').css('display', 'inline-block');
$('#see_all$i').css('display', 'none');
return false;
});
$('#collapse$i').click(function () {
$('#comment2_$i').slideUp('fast');
$('#collapse$i').css('display', 'none');
$('#see_all$i').css('display', 'inline-block');
return false;
})";
Any help would and always will be much appreciated!
Thanks

Generating multiple, slightly different copies of the same jQuery code is not a very good design, IMHO. You should write your jQuery code in a way that it only needs to be in your page once, like:
$('.see_all').click(function(){
var thisItem = $(this);
thisItem.parent().find('.comment').slideDown('fast');
thisItem.parent().find('.collapse').css('display','inline-block');
thisItem.css('display','none');
return false;
});
$('.collapse').click(function(){
var thisItem = $(this);
thisItem.parent().find('.comment').slideUp('fast');
thisItem.css('display','none');
thisItem.parent().find('.see_all').css('display','inline-block');
return false;
})";
Of course, this would also involve adding collapse, see_all, and comment classes to the relevant HTML elements.

html :
<div class="seeall" data-elid="1">...</div>
...
<div class="collapse" data-elid="1">...</div>
...
js :
var $bdy=$(document.body)
$bdy.on("click",".seeall",function(e) {
var el=$(this).css('display','none').data("elid");
$('#comment2_'+el).slideDown('fast');
$('.collapse[data-elid="'+el+'"]').css('display','inline-block');
})
.on("click",".collapse", function(e) {
//same same
});

Related

angular js onmouseover showhint not show data

I have problem with some angular things. I'm trying to set recursive dive with some info with some hintshow (tooltip).
The problem is all information show fine exclusive the hintshow. Added the screen show + some code. The data saved in {{x.who_liked}} as string, for example
"wyd3x, someoneXD, Shohamiko, guymaster, HUBHVNKL, Rauli, Matk, gal350"
<div ng-repeat="x in names | startFrom:currentPage*pageSize | limitTo:pageSize">
<div class="post">
<div class="posterDetails">
{{x.username}}</br>
פירסם ב<?php echo timeAgo("{{x.date}}"); ?>
<div ng-switch on="x.user_id">
<div ng-switch-when="<?=$_SESSION['id']?>">
<?php
echo " <a style='color:red;' href=\"?page=feeds&id={{x.id}}&delete\">(מחק סטטוס)</a>";
?>
</div>
</div>
</div>
<div class="postContent" ng-bind-html="x.msg | unsafe"></div>
<div class="options" style="text-align: right;padding: 5px;">
<div id="like" style="display:inline-block">
<img src="images/{{x.liked}}" post_id={{x.id}} ng-click="like(x)"> {{x.likes}} <div class="liked" onMouseover="showhint('{{x.who_liked}}', this)" style="display:inline-block">אהבו</div>
</div>
<div id="report" ng-click="report(x)" style="cursor: pointer; display:inline-block">
<img src="images/icons/vlag.png"> דווח
</div>
</div>
</div>
current station
tooltip: http://dynamicdrive.com/dynamicindex16/showhint.htm
You shouldn't be using {{}} interpolation inside your onMouseover directive.It should be onMouseover="showhint(x.who_liked, this)"
or
Angular solution
You can fix it like this :
ng-mouseover="showhint(x.who_liked,this)"
Instead of onMouseover , you can use ng-mouseover.
For better understanding , see this fiddle

Php to auto populate grids

I have the following html code:
<div class="media row-fluid">
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/1.png" alt="" />
</div>
<div class="item-info">
Title 1
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/2.png" alt="" />
</div>
<div class="item-info">
This is another title
<p>Some info and details go here.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
</div>
Which basically alternates between a span class with the widget class, and then the widget class without the span3 class.
What I wanted to know was if there was a way to have php "echo" or populate the details for and details under the "item-info" class. Would I need to use a foreach statement to get this done? I would be storing the information in a mysql database, and while I can get it to fill in the info one by one (repeatedly entering the and echoing out each image and item title) it's not practical when the content needed to be displayed is over 15 different items. I'm not well versed in foreach statements so I could definitely use some help on it.
If someone could help me perhaps structure a php script so that it can automatically output the html based on the number individual items in the database, that'd be greatly appreciated!
I'm wondering if the html + php (not including the foreach) would look like this:
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/<? $file ?>" alt="" />
</div>
<div class="item-info">
<?$title?>
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
EDIT:
I wanted to add some more information. The items populated would be based on a type of subscription - which will be managed by a group id.
I was initially going to use <? (if $_SESSION['group_id']==1)>
echo <div class="item-info">
$title
<p>$info</p>
</div>
so that only the subscribed items would populate. But, I would need it to iterate through all the items for group1 table and list it. Currently I know that I can do
<? (if $_SESSION['group_id']==1)
while ($row=mysql_fetch_assoc($sqlItem))
{
$itemInfo = $row['info'];
$image = $row['image'];
$title = $row['title'];
$url = $row['url'];
};
>
$sqlItem for now can only be assigned one thing (manually - as in: $sqlItem = '123'), unless I iterate through which is what I'm trying to figure out.
Just read that 'mysql_fetch_assoc' is being depreciated with 5.5, here is the new way and looks better, easier I think.. Hope this helps, was updated today.
I hope this helps http://php.net/manual/en/mysqli-stmt.fetch.php
replace the printf with echo '//then your html stuff
This will iterate through the rows in your database until their are no more matching records.
shouldn't a while be enough? It depends on the structure of your database and website (we didn't need so much HTML I think. Some more PHP maybe). Hope this helps.

how to pass row from mysql database into div popup window

I need help passing row from a mysql database into a div popup window?
how doI pass product id into popup div which I call through View?
For Example I Want Like This
View</li>
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
You can use new 'echo'
echo 'View</li>';
instead of using this
echo '<li><img src="admin/'.$rowshirt['image'].'" width="225" height="300" alt="" />
View</li>';
combined 'echo' statement for image and link to View
Use jQuery .load function. Then make a hidden div, tgen on an action, use the load function to load the hidden div and change the css selection to visible. Very simple, add effects as well.

php, jquery only first id working normal

I have the following code in .php (code is edited for easier understanding).
while($row = mysql_fetch_array($biznis)){
<div id='listItem'>
<div id='listPic'>
<img src='../../social/images/avatar/empty_avatar_full.jpg'></img>
</div>
<div id='listCont'>
<span>abcde<i>(request pending)</i></span>
</div>
<div id="listInfo">
<span id='info'>More info</span>
</div>
<div style='clear:both;'></div>
<div id='moreInfo'>
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style='clear:both;'></div>
}
So the code is nothing special, I just get all records from the table lined up.
So now, here is the problem, as you can see, I use the same ids for the same elements.
Jquery code
$(document).ready(function () {
$("#moreInfo").hide();
$("#info").click(function(){
$("#moreInfo").slideToggle();
});
});
OK so here are now the problems. Only first div (#moreinfo) is hidden, all the others are shown. And only first span (#info) is toggling the slider.
I tried with putting the counter for ids, and jquery each function, but nothing realy worked as i imagined.
Ty, Sebastian
Element IDs should be unique identifiers for that element. Use classnames for generic selectors:
<? while($row = mysql_fetch_array($biznis)): ?>
<div class="listItem">
<div class="listPic">
<img src="../../social/images/avatar/empty_avatar_full.jpg"></img>
</div>
<div class="listCont">
<span>abcde<i>(request pending)</i></span>
</div>
<div class="listInfo">
<span class="info">More info</span>
</div>
<div style="clear:both;"></div>
<div class="moreInfo">
<span>Invitation sent: xx-yy-zzzz</span>
</div>
</div>
<div style="clear:both;"></div>
<? endwhile; ?>
And change your jQuery to:
$(function() {
$('.moreInfo').hide();
$('.info').click(function() {
$('.moreInfo').slideToggle();
});
});
Edit
$('.info').click(function() {
$(this).parent().siblings('.moreInfo').slideToggle();
});
use class and not ids because you're in a while loop. An id exists only once.
And update your JS.

How To: PHP + Javascript Pop-up Window

I have the following code that gives me a list of items on the front-end, but I want those links to open in a new pop up window. Is there a way I can do this?
Thank you.
echo '<div class="item_row_header">';
for($i=0;$i<count($this->fields);$i++) {
echo '<div class="item_cell jdheader'.$this->fields[$i]->cssclass.'">';
//to display the header with/without sorting option
if(in_array($this->fields[$i]->type, array(10,11,12,13)))
echo $this->fields[$i]->name;
else
echo JHTML::_('jdgrid.sort', $this->fields[$i]->name, 'field_'.$this->fields[$i]->id, #$this->cparams->filter_order_Dir, #$this->cparams->filter_order );
echo '</div>';
}
echo '<div class="clr"></div></div>';
}
echo '<div class="itemlist itemlist_type'.$this->type->id.'">';
if(count($this->items)) {
//all the item list part display here
for($i=0;$i<count($this->items);$i++) {
$item = $this->items[$i];
require(dirname(__FILE__).DS.'default_item.php');
}
Here is what I get on the front-end with the code above:
<div class="item_row_header">
<div class="item_cell jdheader">Título</div>
<div class="item_cell jdheader">Fotos</div>
<div class="item_cell jdheader">Cidade</div>
<div class="item_cell jdheader">Estado</div>
<div class="item_cell jdheader">Tipo do Imóvel</div>
<div class="item_cell jdheader">Valor R$</div>
<div class="clr"></div>
</div>
<div class="itemlist itemlist_type15">
<div class="item_row_bg featured itemrow_type15">
<div class="item_content">
<div class="item_cell ">Temporada Destaque</div>
<div class="item_cell "><img src="http://mysite.com.br/joomla/images/joomd/thumbs/1350654862temporada-destaque.jpg" alt="Fotos" /></div>
<div class="item_cell ">Exemplo de Cidade</div>
<div class="item_cell ">São Paulo</div>
<div class="item_cell ">Casa</div>
<div class="item_cell ">600</div>
<div class="clr"></div>
</div>
</div>
<div class="item_row itemrow_type15">
<div class="item_content">
<div class="item_cell ">Temporada</div>
<div class="item_cell "><img src="http://mysite.com.br/joomla/images/joomd/thumbs/1350654792temporada.jpg" alt="Fotos" /></div>
<div class="item_cell ">Exemplo de Cidade</div>
<div class="item_cell ">São Paulo</div>
<div class="item_cell ">Casa</div>
<div class="item_cell ">800</div>
<div class="clr"></div>
</div>
</div>
</div>
Here is how I solved it:
I opened the default_item.php and found this:
if($j==0) {
echo '<a href="'.JRoute::_('index.php?option=com_joomd&view=item&layout=detail&typeid='.$item->typeid.'&id='.$item->id).'">';
echo $this->field->displayfieldvalue($item->id, $this->fields[$j]->id, true);
echo '</a>';
}
Then I could call the pop up using jQuery [http://swip.codylindley.com/popupWindowDemo.html][1]
Here is how my working code looks like now:
echo '<a class="propriedade" href="'.JRoute::_('index.php?option=com_joomd&view=item&layout=detail&typeid='.$item->typeid.'&id='.$item->id).'">';
Thank you all so much for the help.
As Alex noted, this isn't possible with just PHP. PHP is a server-side language that happens before the page is loaded. You can achieve this with HTML and JavaScript.
You should look into the JavaScript function open() which allows you to open a new window with an exact width/height and URL specified.
Read more about open() here
EXAMPLE CODE :
myWindow=window.open('http://google.com','','width=200,height=200')
This will open Google in a new window with dimensions of 200x200 pixels
To use this code, you must insert this in either a <script> tag or in an external JavaScript file. I suggest using this as a function like so:
function newWindow(url, width, height)
{
myWindow=window.open(url,'','width=' + width + ',height=' + height);
}
You can then call this with:
newWindow('http://google.com', 200, 200)
you can implement this in your code with the onCLick attribute:
Click Me
Demo of my function with HTML and JavaScript
The reason I suggest this function is that using the target="_blank" attribute on some browsers just opens in a new tab, which you can use, but I assumed you wanted a whole new window
Now that you have added some code to your question, I can see you have a lot of list items, you can simple add an anchor tag to make my function work, example:
<div class="item_cell "><img src="http://alii.com.br/joomla/images/joomd/thumbs/1350654792temporada.jpg" alt="Fotos" /></div>
<div class="item_cell ">Exemplo de Cidade</div>
<div class="item_cell ">São Paulo</div>
<div class="item_cell ">Casa</div>
<div class="item_cell ">800</div>
would become:
<div class="item_cell "><img src="http://alii.com.br/joomla/images/joomd/thumbs/1350654792temporada.jpg" alt="Fotos" /></div>
<div class="item_cell ">Exemplo de Cidade</div>
<div class="item_cell ">São Paulo</div>
<div class="item_cell ">Casa</div>
<div class="item_cell ">800</div>
add target="_blank" in tour tag or use window.open method.
e.g.:
Google
or
<script>window.open('http://google.com','','width=200,height=200'); </script>

Categories