This code is broken because I am nesting php code blocks. What is the proper way to do this?
<?php
if ($prev_ID > $totalRows_totalBoogerRows)
{
echo "";
}
else
{
echo <<<_END
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=<?php echo $prev_ID; ?>'>
<h4 style='text-align: right;'>Ep. <?php echo $prev_ID; ?>
</h4>
</a>
</div>"
_END;
}
?>
I breaks on line 11 because my closing PHP tag closes the first PHP opening tag on line one (I want it to only close the opening PHP tag on line 11). Also, I may be using <<<_END wrong.
When you are echoing a string that needs php variables inserted, do not open and close php tags but use string concatenation:
<?php
if ($prev_ID > $totalRows_totalBoogerRows)
{
echo "";
}
else
{
echo "
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=".$prev_ID."'>
<h4 style='text-align: right;'>Ep. ".$prev_ID."</h4>
</a>
</div>";
}
?>
So why don't you use this?
<?php if(something): ?>
HTML
<?php else: ?>
HTML
<?php endif ?>
So:
<?php if ( $prev_ID <= $totalRows_totalBoogerRows ): ?>
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=<?php echo $prev_ID ?>'>
<h4 style='text-align: right;'>Ep. <?php echo $prev_ID ?></h4>
</a>
</div>
<?php endif ?>
Your heredoc terminating token _END is in wrong place. There must not be any white space before it.
See the warning from PHP.NET manualString.Syntax.Heredoc.
It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.
Also when you use heredoc the string gets evaluated as double quoted string. So you can use variables inside like this.
echo <<<_END
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=$prev_ID'>
<h4 style='text-align: right;'>Ep. $prev_ID
</h4>
</a>
</div>
_END;
Just close and reopen PHP tags
<?php
if ($prev_ID > $totalRows_totalBoogerRows)
{
echo "";
}
else
{?>
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=<?php echo $prev_ID; ?>'>
<h4 style='text-align: right;'>Ep. <?php echo $prev_ID; ?>
</h4>
</a>
</div>
<?php}
?>
try this ,
<?php
if ($prev_ID > $totalRows_totalBoogerRows)
{
echo "";
}
else
{
echo <<<_END
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID='.echo $prev_ID;'>
<h4 style='text-align: right;'>Ep. echo $prev_ID;
</h4>
</a>
</div>"
_END;
}
?>
try this :
if ($prev_ID > $totalRows_totalBoogerRows)
{
echo "";
}
else
{
echo "
<div class='paging_button_left paging_button_left_episode'>
<a href='episode.php?post_ID=\"$prev_ID\"'>
<h4 style='text-align: right;'>Ep. \"$prev_ID\"
</h4>
</a>
</div>
";
}
Related
I have the following code.
while ($slideNews = mysqli_fetch_array($slideImage )) {
$cutNews = LimitarCaracteres($slideNews['textNews'], $maxCaracteres = 300);
?>
<li>
<a href="<?php echo ROOT;?>/News/<?php print $slideNews["id"].'/'.$slideNews["friendlyURL"];?>">
<span>
<?php echo $cutNews; ?>
</span>
<h2 class="nomeGame"><?php print $slideNews["game"];?></h2>
<img src=" <?php echo $slideNews['secondImage']; ?> ">
</a>
</li
<?php } ?>
I need to do that in this part of the code:
<h2 class="nomeGame"><?php print $slideNews["game"];?></h2>
html does not run, I just need the part of the text
Well im having this problem and cant resolved it would like to have some help from you guys, so here it is:
I have this code:
<?php if (mysql_num_rows($tournaments) !=0){
do { ?>
<div id="mainContainer">
<div id="leftContainer">
<img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png">
</div>
<div id="rightContainer">
<div id="rightContent">
<p><?php echo $row_tournaments['description']; ?></p>
<a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>">
<div id="galleryButton">
<p>Entrar no torneio</p>
</div>
</a>
</div>
<div id="rightDetails">
<i class="fa fa-gamepad"></i>
<a href="games.php?id_game=<?php echo $row_tournaments['id_game']; ?>">
<?php echo $row_tournaments['game']; ?>
</a>
<br>
<i class="fa fa-calendar-o"></i>
<a href="#">
<?php echo $row_tournaments['date']; ?>
</a>
<br>
<i class="fa fa-pencil-square-o"></i>
<a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>#disqus_thread">
Sem comentários
</a>
<br>
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i>
<a onClick="giveThatInputAValue()">
<?php echo $row_tournaments['city']; ?>
</a>
<br>
<img src="images/<?php echo $row_tournaments['online']; ?>.png">
<a href="#">
<?php echo $row_tournaments['online']; ?>
</a>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults">
<p>Sem torneios</p>
</div>
<?php } ?>
Everything loop fine but this part is not looping at all:
<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();*/
}
</script>
And dont know why is it, someone please help. Cumps.
You are defining your function in a loop. Over and over again. So when you call your function, which one should it call? Probably all previous ones have been overwritten by the last version.
You should define your function outside of any loop and then call it with the parameters that you have available in the loop.
Apart from that you are also adding elements with ID's in your loop. ID's need to be unique as well so you should use classes or give them a unique ID.
a real php programmer knows this (meaning i am not one). been a while away from php.
ok first as i see it you never get your first row from result set (someone advise here). you are checking to see if you have rowcount then plod about. i reserve the right to be very wrong.
try something like
if ($result) {
while($row = mysql_fetch_array($result)) {
// output $row info
}
}
else {
echo "nada";
}
put your script for the function at the top
don't surround the main div section creations like you are with a loop
I'm trying to generate dynamic contents fill from mysql db
here is my php code:
<?php
include 'header.php';
error_reporting(1);
$user = "root";
$pass = "";
$dbName = "haimi";
mysql_connect('localhost', $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
$sql = "SELECT * FROM Projects ";
$result = mysql_query($sql);
?>
<?php
while ($line = mysql_fetch_array($result)) {
?>
<li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php
}
?>
The li displays correctly, but the following does not:
<div class="row projects m0">
<?php
while ($line = mysql_fetch_array($result)) { ?>
<div class="project mix catHouses">
<div class="tint"></div>
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-
lightbox="project" data-title="Central Hospital (building)">
<img src="images/projects/".<?php echo
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"
class="projectImg"> </a>
<div class="projectDetails row m0">
<div class="fleft nameType">
<div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
<div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
</div>
<div class="fright projectIcons btn-group" role="group">
<a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital (building)" class="btn btn-default">
<i class="fa fa-link"></i></a>
<i class="fa fa- search"></i>
</div>
</div>
</div>
<?php
}
?>
</div>
It data in the divs doesn't appear.
You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:
//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );
The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.
Important Note
The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).
You have some HTML mistakes here:
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>
First, there is no need to use . operator (as you are in HTML, not PHP),
Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:
<a
href="images/projects/<?php echo $line['ProjectImage1']; ?>"
data-lightbox="project"
data-title="Central Hospital (building)"
>
<img
src="images/projects/<?php echo $line['ProjectImage1']; ?>"
alt="<?php echo $line['ProjectTitle']; ?>"
class="projectImg"
>
</a>
You will get older fast writing code like that ;)
How about this:
while ($line = mysql_fetch_array($result)) {
$category = $line['Category'];
echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}
Like what Mr #matthew said
I was making a single call, and I was trying to loop through it twice.
The problem solved with this code before the while loop:
$result = mysql_query($sql);
I have a problem, when I try to echo a div class. When I put the following code in my template, it detects only the closing tag and not the opening. So, my webpage is ruined:
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
<br />
<div class="rw-ui-container"></div>
<br /><br />
<?php
var_dump($_GET['categorie']);
if(isset($_GET['categorie']) && $_GET['categorie'] === "navigatie_bar")
{
echo "<div class=\"fb-comments\" data-href=\"http://alledaagsetips.nl\" data-numposts=\"10\" data-colorscheme=\"light\"></div>";
}
?>
</div> <!-- end pagina_text -->
</div><!-- end container -->
Does someone know what am I doing wrong?
Replace single quote with double quote.
Change
<?php
if(strcmp($_GET['categorie'], "navigatie_bar") != 0)
{
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
to this
<?php
if(strcmp($_GET['categorie'], 'navigatie_bar') != 0)
{
echo "<div class='fb-comments' data-href='http://alledaagsetips.nl' data-numposts='10' data-colorscheme='light'></div>";
}
?>
Are you sure the "IF" condition is working?
Perhaps with a simple string comparison like so :
<?php
if(isset($_GET['categorie']) && $_GET['categorie'] === "navigatie_bar") {
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
replace single quotes with double and vice versa
echo "<div class='fb-comments' data-href='http://alledaagsetips.nl' data-numposts='10' data-colorscheme='light'></div>";
or use the same for all but with backslash, just to not get confused
echo "<div class=\"fb-comments\" data-href=\"http://alledaagsetips.nl\" data-numposts=\"10\" data-colorscheme=\"light\"></div>";
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
<br>
<div class="rw-ui-container"></div>
<br><br>
<?php if ( ! empty($_REQUEST['categorie']) and $_REQUEST['categorie'] == 'navigatie_bar'): ?>
<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>
<?php endif; ?>
</div> <!-- end pagina_text -->
</div><!-- end container -->
i have to modify this code, to echo every 4 thumbs (extraimage) inside a div...
I searched on Stackoverflow but all answers talk about setting a counter, i want to avoid this (if possible) using a counter that is already counting the extraimages.. i think it most be as easy as a conditional
if counter extraimages==3 echo div.. but how do i go back to 0 ,, or maybe i am missunderstanding the way to do this..
This is the part of the code where the array is set and the for each is set.
<?php if($extraimagecount >0){?>
<?php foreach ($extraimage as $key=>$value){?>
<?php }?>
<?php }?>
<a href="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" >
<img src="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" class="minis"/>
</a>
<?php if($extraimagecount >0){
$rotate=1;
$tumppr=0;
?>
<?php foreach ($extraimage as $key=>$value){
$rotate=$rotate+1;
?>
<a href="<?php echo DATA_DIR."/".$id."/".$value['image'];?>" >
<img src="<?php echo DATA_DIR."/".$id."/t_".$value['image'];?>" class="minis"/>
</a>
<?php
if($rotate==8)
{
$rotate=0;
$tumppr=$tumppr+1;
?>
<?php
}
?>
<?php }?>
<?php }?>
</div>
<?php
$lftstr="";
$rgtstr="";
if($extraimagecount >0)
{
$extcnt=count($extraimage);
$extcntnew=$extcnt+1;
$extdivide=intval(($extcntnew/8));
$extmode=($extcntnew % 8);
for($i=0;$i<$extdivide;$i++) //************ For Right Arrow ***************/
{
?>
<div id="rgt_<?php echo $i;?>" class="rgt" <?php if($i >0 || $extmode ==0){?>style="display: none;"<?php }?> ><img class="rgtimg" src="images/rnext.png"></div>
<?php
$rgtstr=$rgtstr.$i.'_';
}
for($ii=1;$ii<$extdivide;$ii++) //************ For left Arrow ***************/
{
?>
<div id="lft_<?php echo $ii;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$ii.'_';
}
if($extmode >0)
{
?>
<div id="lft_<?php echo $extdivide;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$extdivide.'_';
}
}
?>
Its actually quite easy, use the % operator. a%b will return the remainder of a/b. heres how you use it
for($i=0;$i<9;$i++)
{
echo $i%3." ";
}
this will print out
0 1 2 0 1 2 0 1 2
You can then use this to create groups of 4 in your case.