I'm trying to implement a simple javascript/jquery-1.6.2 slideshow (I'm not used to javascript), the problem I have is that the items are all shown and I don't know how to hide them.
here is my PHP/HTML code:
<?php
$c = 0;
// show parts of latest news:
foreach ($items AS $data) {
// news image
$image = './images/'.$data['image'];
// description
$part_description = substr(nl2br($data['content']), 0, 120);
?>
<div id="cap_<?php echo $c; ?>" class="cap" >
<h3><a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
<?php echo $data['title']; ?></a></h3>
<table>
<tr>
<td>
<a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
<img alt="Item image" width="200"
src="<?php echo $image; ?>" />
</a>
</td>
<td>
<strong>By</strong>
<?php echo $data['author']; ?>
<br/>
</td>
<tr/>
</table>
<p>
<?php echo stripslashes($part_description); ?>
<a href="<?php echo WEBSITE.'/news/view?id='.$data['id']; ?>" >
read more
</a>
</p>
</div>
<?php
$c++;
}
// show links:
?>
<div class="links">
<?php
for ($i=0; $i<$k; $i++) {
?>
<a href="javascript:slideShow(<?php echo $i; ?>, <?php echo $k; ?>);" class="link_number" id="link_<?php echo $i; ?>">
<?php echo $i+1; ?>
</a>
<?php
}
?>
And my js function:
function slideShow(id, max) {
var inputHide = null;
for (var i=0; i<max; i++) {
$('#cap_'+id).fadeOut(500);
inputHide = document.getElementById('cap_'+i);
document.getElementById('link_'+i).style.backgroundColor = '#999999';
}
$('#cap_'+id).fadeIn(500);
document.getElementById('link_'+id).style.backgroundColor = 'black';
}
I have two questions:
Q1: how can I fix this problem and show only one
Q2: is it better if I use a ready-to-use script like jqueryui?
In terms of your second question, It depends on what you need, and your understanding of JS. If you have a pure JS script, and you understand how and why it works, you can tailor it to your needs more. 'personalise it', if you like. However if your knowledge of JS is limited, then a ready to use script may suit you better.
A good way to learn things is to find a pure JS script somewhere (probably many on here), and use that one, but do your research and use your initiative until you understand how it works. Then you can use it in other projects, change it, use bits of it. That's how I learnt most of my javascript!
I've had some trouble with slideshows recently too, good luck! :)
Q1: this should work: inputHide.style.display = 'none';
Q2: In my opinion, both ways are good
Related
<div class="priceStruct" id="priceStruct">
<h3>Pallet Cost Structure</h3>
<?php $i=1;
foreach($price_structure as $price_structure_list) {
$palletDetails = $price_structure_list['min']." - ".$price_structure_list['max'];
?>
<div class="display_content" id="price<?php echo $i; ?>">
<span class="first price" id="first<?php echo $i; ?>" ><?php echo $palletDetails." Pallets";?></span>
<span class="second price" id="second<?php echo $i; ?>">£<?php echo $price_structure_list['price'];?></span>
<span class="third price"><a href="#" class="breakDown" >Click Here for Break Down</a></span>
</div>
<?php $i++; } ?>
</div>
Try this. Class should be second_price instead of second price.
Dont forget to include jQuery library
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
$(function(){
$(".breakDown").click(function(){
spanValue = $('.second_price').text();
alert(spanValue);// just to check
});
});
Solution 1:
Add this to your a tag:
onclick="alert($('#second<?php echo $i; ?>').text());"
So that it will result in:
Click Here for Break Down
Solution 2:
You can also use jQuery's parent and siblings to find the correct span tag:
$(document).ready(function() {
$('.breakDown').click(function() {
alert($(this).parent().siblings('.second').text());
});
});
The disadvantage of solution 2 is that you'll run in problems when the structure of your HTML code is changed (the a tag is no longer not a child of span etc.)
<?php
$name = "{{t_33}}";
$y=$name;
for($i=$y;$i<24;$i++) { ?>
<li>
<a class="<?php
if (($i>0 and $i<4) || ($i>20 and $i<24)) echo 'dark';
else if (($i>3 and $i<8)|| ($i>16 and $i<21)) echo 'light';
else if ($i>7 and $i<17) echo ''; ?>"
href="#">
<?php echo $i; ?>
</a>
<?php if($i==1) { ?>
<h4>{{day}}</h4>
<?php } ?>
</li>
<?php } ?>
The issue is that I am getting the value in $name but when I assign it in $y and try to use it in for loop the page goes blank. Please help.
I think that issue is with order of processing your code. You should know that php is interpreted on server side and javascript is procesed on client side.
Now when your angular is code running, php functions are already processed. (I'm simplyfing here ofc.)
I have created a homepage editor tool in a script I purchased. The function of this homepage editor is to allow me to create different sections and display them one on top of the other in the order they are created. Which in hopes will give me an effect of several blocks that stretch width of the screen.
All seems to work well except one piece. I input my html and php code into the field in the admin panel and it saves to the db as I wrote it. However, when I go to echo each section back to the homepage it just displays my php code as plain text and doesn't interpret it as php and do its function.
Here is code from the homepage.php that prints the results.
<?php
session_start();
require_once("inc/config.inc.php");
if (isset($_GET['ref']) && is_numeric($_GET['ref']))
{
$ref_id = (int)$_GET['ref'];
setReferal($ref_id);
header("Location: index.php");
exit();
}
/////////////// Page config ///////////////
function get_all_section($section_id='')
{
$sql="SELECT * FROM `cashbackengine_homepage` WHERE 1";
if($section_id!="")
{
$sql.=" AND section_id='".$section_id."'";
}
$sql.=" AND section_status=1";
$sql.=" ORDER BY section_order ASC";
//echo $sql;
$res=mysql_query($sql);
while($row=mysql_fetch_array($res))
{
$section_array[]=array(
'section_id' =>$row['section_id'],
'section_name' =>$row['section_name'],
'section_desc' =>$row['section_desc'],
'section_order' =>$row['section_order'],
'section_status' =>$row['section_status'],
'last_updated' =>$row['last_updated'],
);
}
return $section_array;
}
$get_all_section=get_all_section('');
/*$get_all_section2=get_all_section('2');
$get_all_section3=get_all_section('3');
$get_all_section4=get_all_section('4');
$get_all_section5=get_all_section('5');*/
for($i=0; $i<count($get_all_section);$i++)
{
//echo htmlspecialchars_decode($get_all_section[$i]['section_desc']);
//echo htmlspecialchars_decode(stripslashes(str_replace(" ","",(str_replace("<br />","\n",$get_all_section[$i]['section_desc'])))));
echo $get_all_section[$i]['section_desc'];
}
?>
I am certain the problem has to do with the echo at the end. But I am unsure how to use htmlspecialchars to make it work with php if it even will. Or if I have to put something weird in my saved section.
Here is one of my sections. Any help is greatly appreciated. Thank you.
<div style="height:260px; width:100%; background-color:#000; margin:0px; color:white;">
<div id="header">
<div id="logo"><img src="<?php echo SITE_URL; ?>images/logo.png" alt="<?php echo SITE_TITLE; ?>" title="<?php echo SITE_TITLE; ?>" border="0" /></div>
<div class="start_saving">
<div id="links">
<?php if (MULTILINGUAL == 1 && count($languages) > 0) { ?>
<div id="languages">
<?php foreach ($languages AS $language_code => $language) { ?>
<img src="<?php echo SITE_URL; ?>images/flags/<?php echo $language_code; ?>.png" alt="<?php echo $language; ?>" border="0" />
<?php } ?>
</div>
<?php } ?>
<div id="welcome">
<?php if (isLoggedIn()) { ?>
<?php echo CBE_WELCOME; ?>, <span class="member"><?php echo $_SESSION['FirstName']; ?></span><!-- | <?php echo CBE_ACCOUNT ?>--> | <?php echo CBE_BALANCE; ?>: <span class="mbalance"><?php echo GetUserBalance($_SESSION['userid']); ?></span> | <?php echo CBE_REFERRALS; ?>: <span class="referrals"><?php echo GetReferralsTotal($_SESSION['userid']); ?></span>
<?php }else{ ?>
<a class="signup" href="<?php echo SITE_URL; ?>signup.php"><?php echo CBE_SIGNUP; ?></a> <a class="login" href="<?php echo SITE_URL; ?>login.php"><?php echo CBE_LOGIN; ?></a>
<?php } ?>
</div>
</div></div>
</div>
It looks like you're getting these section contents pieces out of your database, and not from a file stored on your web server. Is that correct?
Assuming that's true, then my next question would be, who populates this data? Is this taken in any way from user input? The reason why I ask is because of my next suggestion, which may or may not be received well.
The reason why your PHP code isn't executing, is because it's being retrieved from the database and output as a string, not as code. So how do you execute code that's stored in a string, you ask? Well, the answer to that question is to use eval() on the string. But this is where you have to be really careful!!!!!!! If any part of that string could have possibly come from an untrusted source, then malicious PHP code could be executed, which could potentially give evildoers a way into your server, where they can find all the information in your database, server, etc. Make sure you know where your code is coming from before executing it!
You make a good point that it's HTML mixed with PHP. So I see two possible solutions...
This post suggests that you could do eval(' ?>'. $section .' <?php'); This makes sense, you're breaking out of PHP before you eval your string, and so requiring the included string to open its own PHP tags to write PHP code.
Another way I can think of would be to throw the contents into a temporary file, and then include() that file:
// get contents, store in $contents
$filename = tempnam(sys_get_temp_dir(), 'section');
file_put_contents($filename, $section);
include($filename);
unlink($filename);
I'm attempting to modify an app for facebook that uses php and javascript.
It allows a user to send gifts to each other's wall. When a gift is received, it show's on the app's notification page for that user.
The problem: after a gift is accepted, it still shows on the notifications page. I suppose because it was deemed necessary in order for the point system as well as the history functions to operate properly. It appears that gifts older than 30 days are no longer displayed. I tried to permanently change the date of the gift after it's accepted.
Can anyone please suggest possible ways to:
Option 1. After the gift is accepted, permanently change it to a different image to inform the user "you have already accepted this". Without removing it from the database.
Option 2. Remove the gift from the page (or prevent it from being displayed) after it's accepted...without deleting it from the database. .hide and display:none; are not viable options because the gift will be displayed again...every time the page loads.
Option 3. Last and least desired option, remove the gift from the page and database as well.
The original code for that page is below. The part of the code that I'm guessing needs to be modified is located between the asterisk dividers. I hope it's not too difficult to follow :-/
Thanks for your time.
<?php
if(!isset($facebook))
{
$user_id=$_POST['user_id'];
}
else
{
$user_id=$user_info['UserID'];
}
$news_limit=$current_time-2592000;
$history_query=mysql_query("SELECT * FROM ".$exchange_table." WHERE Time>'$news_limit'
AND ReceiverID='$user_id' ORDER BY Time DESC");
$history_num=mysql_num_rows($history_query); ?>
<div style="font-size:14px;width:720px;padding:10px;<?php echo $background_one; ?>"
align="left">
<div style="font-weight:bold;font-size:16px;color:#6f6f6f;">
<?php echo $nav_info['home']; ?>
</div>
<div style="width:100%;margin-top:10px;" align="center">
<img src="<?php echo $app_info['image_url']; ?>divider.gif">
<img src="<?php echo $app_info['image_url']; ?>divider.gif">
<img src="<?php echo $app_info['image_url']; ?>divider.gif">
</div> <?php
if($history_num>0)
{
$gift_query=mysql_query("SELECT GiftID,Name,Image FROM ".$gift_table." WHERE
Type='1'");
while($gift_info=mysql_fetch_assoc($gift_query))
{
$gift_array[$gift_info['GiftID']]['name']=$gift_info['Name'];
$gift_array[$gift_info['GiftID']]['image']=$gift_info['Image'];
} ?>
<div style=""> <?php
$i=1;
while($history_info=mysql_fetch_assoc($history_query))
{ ?>
<div style="margin-top:10px;">
<div style="float:left;width:75px;">
<img src="<?php echo
$app_info['upload_url'].$gift_array[$history_info['GiftID']]['image']; ?>"
style="width:75px;height:75px;">
</div>
<div style="float:left;margin-left:10px;">
<span style="font-size:18px;"><span id="month<?php echo $i; ?>"></span> -
Gift Received!</span><br>
You received a(n) <b><?php echo $gift_array[$history_info['GiftID']]
['name']; ?></b> from <b><fb:name uid='<?php echo $history_info['FacebookID']; ?>'
linked='false'></fb:name></b>!<br>
**************************************************************
<input id="share_button<?php echo $i; ?>" onclick="shareFeed('<?php echo
$gift_array[$history_info['GiftID']]['image']; ?>','<?php echo $history_info['GiftID'];
?>','<?php echo $gift_array[$history_info['GiftID']]['name']; ?>')" type="image" src="
<?php echo $app_info['image_url']; ?>share_button.png"
onmouseover="convert('share_button<?php echo $i; ?>','share_hover.png')"
onmouseout="convert('share_button<?php echo $i; ?>','share_button.png')"
style="height:29px;margin-top:5px;">
</div>
**************************************************************
<br style="clear:both;">
</div>
<script>
var date_text="";
var time_date=new Date();
var monthNames=
["January","February","March","April","May",
"June","July","August","September","October","November","December"];
time_date.setTime(<?php echo $history_info['Time']*1000; ?>);
document.getElementById('month<?php echo $i; ?
>').innerHTML=monthNames[time_date.getMonth()]+" "+time_date.getDate();
</script> <?php
$i++;
} ?>
</div> <?php
}
else
{ ?>
<div style="font-size:14px;font-weight:bold;margin-top:20px;" align="center">
No giftss received over the past 30 days.
</div> <?php
} ?>
</div>
I am creating a element dynamically in html and want to use it in a javascript function by referring to its ID. But as its ID is dynamically generated using PHP , so how do i pass it to the javascript function into getElementById?
Actually , i am trying to do pagination using javascript. I have displayed Page No.s, and put HREF on them, but onclick function am stuck up? Please help. My code is as below:
<?php
echo 'Page';
while($d>0)
{
?>
<input type="hidden" value="<?php echo $z; ?>" name="pagination" id="<?php echo 'pagination' .$z ; ">
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked(); submit();" >
<?php echo $z; ?>
</a>
</label>
<?php
$z++;
}
?>
in your javascript functio you can use the same PHP expression to get the id
var id="<?php echo '\"' .$z '\"'; >";
or you can have it like this,
<script type="text/javascript" language="javascript">
<!--
<?php
echo("id= $z;");
?>
// -->
</script>
to get the id
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked('<?php echo "\"" ."labelofpagination".$z ."\""; ?>'); submit();" >
<?php echo $z; ?>
</a>
</label>
and javascript
function PaginationLabelClicked(id){
// you can get the id here...
}