print html inside two foreach circles - php

I've been dealing with some calendar code for a while and i can't seem to find the way..
So, i have this calendar showing tours(Y), months(X) and the respective prices for each combination. Now what i need is to add additional column showing the durations in days of each tour. I did that, and it works fine but i also have to sort them and group by the number of days.
e.g First column (containing the name of the tours) starts by title : "1 day tours" and is followed by the tours whom's duration is 1 day. Than an other title: "2 days tours"..and so on.
To do that i had to create two embed 'foreach' circles. The main 'foreach' displays the titles " X days tours",, than the embed 'foreach' displays the proper tours.
Now my problem is how do i print out the html code????
here is the code:=>
foreach($tour_durations as $d){
**print "<tr><td colspan="4"><b>Tous of <?php echo $d;?> Days</b></td></tr>"**
foreach($result->tour as $tour) {
if($tour->duration == $d){ ?>
<tr>
<td>
<a href="<?php print $tour->tour_url_tracked; ?>"><?php print $tour->tour_name;?>
</a></td>
<?php
print "<td><span class='month of'>".$tour->duration."</span></td>";?>
<= the enclosed by stars, is the problematic code..
What is the rights way to display it?? What's wrong??
I hope i was enought clear! :/
Thanks in advance!!!

echo "<tr><td colspan='4'><b>Tous of ".$d." Days</b></td></tr>";
instead of
print "<tr><td colspan="4"><b>Tous of <?php echo $d;?> Days</b></td></tr>"
should do it

Related

Want to break the loop after 10 rows to show a banner and than continue the loop were it stopt using PHP

I have seen some solutions already for tables but for some reason the break also duplicated which only need to be shown once
i have the following code:
<?php
while($result=mysqli_fetch_array($query)){ echo "<div class='col-lg-12'><div class='panel panel-default'>".$result['brand']." ".$result['modelyear']." ".$result['type']."<img src='".$result['link_image']." heigth:'50px' class='img-responsive'></div></div>";}
?>
I would like to try to break it after 10 rows and than show a div with a banner in it.
i tried the following PHP add html break after every 10th mysql result
however the banner also multiplies it self.
and yes i am a noob
Regards
Bas
You can make a variable that increment every time the loop execute. After the condition met you can display your div and end the loop. Something like this.
$i=0;
while($result=mysqli_fetch_array($query)){
if($i % 10 == 0){
echo "<div>Banner Div</div>";
} else {
echo "<div>Normal Div</div>";
}
$i++;
}

php mysql display table

I am a beginner and somehow made to get the query (php & Mysql) I want and using echo i got the output as few lines without difficulty. But now I want the output inside the cell of a table. I tried something like this:
This does not work:
<tr>
<th>subject</th>
<th>grade</th>
</tr>";
echo "<tr>";
echo "<td>".$Row['name1']."</td>;
echo "<td>".$Row['subject1'].</td>";
echo "</tr>";
echo "</table>";
Whereas this work:
echo $line['name1']."<tr></td>"."";
echo $line['subject1']."<tr></td>"."";
The echo $line statement echoes the value of name1 and subject1 without any difficulty. but the echo Row is not showing the output. As my data has only one row I dont have to use any loop. I actually want two fields in first row (name1 and subject1) and then in next row the fields of name2 and subject2 and till name7, subject7. It looks like the format inside the table is wrong. Could someone help me plz?
First of all replace
echo "<td>".$Row['name1']."</td>;
with
echo "<td>".$Row['name1']."</td>";
you are missing (") at the end before (;)
Updated with the missing table tag. Try this
<?php
echo '<table>';
echo '<tr>';
echo '<th>subject</th>';
echo '<th>grade</th>';
echo '</tr>';
echo "<tr>";
echo "<td>".$Row['name1']."</td>";
echo "<td>".$Row['subject1']."</td>";
echo "</tr>";
echo "</table>";
?>
Just to expand the current answers, I'd suggest you use a single echo and concatenate the strings or even better, just use one single string and concatenate only the necessary variables:
<?php
echo '
<table>
<tr>
<th>subject</th>
<th>grade</th>
</tr>
<tr>
<td>'.$Row['name1'].'</td>
<td>'.$Row['subject1'].'</td>
</tr>
</table>';
?>
This of course works better if the amount of PHP code is greater than the amount of HTML code. But if you were to write more HTML than PHP, it'd make more sense to just open and close <?php?> tags and echoing the variable you want.
I used an answer instead of a comment for the sake of the example. Feel free to try this approach when you are dealing with several html elements and need to insert your values within them.

Only first word is fetched and displayed from a sentence

I have two pages index.php and currentmovies.php. I am trying to display movie name for a given movie in currentmovies.php, but I am unable to display the entire name, only the first word is being displayed. I have written my query in index.php and I am passing the values to currentmovies.php.
Eg: if the movie name is "Lord Of the Rings" only "Lord" is being displayed
//Code in index.php
echo "";
//code in currentmovies.php
<?php
echo "<b><font size='3'>Current Movie:",$_GET['movie_name']," </font></b>";
echo "<p> The movie ",$_GET['movie_name']," was released on ",$_GET['rdate']," and falls under the ",$_GET['genre']," category. </p>";
Thanks for the help
//code in currentmovies.php
<?php
echo "<b><font size='3'>Current Movie: ".$_GET['movie_name']."</font></b>";
echo "<p>The movie ".$_GET['movie_name']." was released on ".$_GET['rdate']." and falls under the ".$_GET['genre']." category.</p>";
Try this:
Pleae do it this way:
<?php
echo '<b><font size="3">Current Movie: '.$_GET['movie_name'].' </font></b>';
echo '<p> The movie '.$_GET['movie_name'].' was released on '.$_GET['rdate'].' and falls under the '.$_GET['genre'].' category. </p>';
Please remember, in this case dot instead of comma.
And one more thing:
If you don't use any variables inside quota, please use single quota, plus for html is much better and easier use single quota, in example:
<font size="3">

ForEach multiply issue

This is probably a stupid gotcha that I'm overlooking but I'm hoping one of you can help me!
I've got a loop to list a grid of Products in my DB.
So far so good, everything is displaying roughly OK except this one little issue.
Within a list I'm doing the following:
<ul>
<?php $i=0; foreach ($products as $product) : $i++; ?>
<li <?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
// Then I've got the image thumbnail etc coming in....
All looks good except for the LAST row...
So for instance if I have 8 products... the first 7 will display on the page correctly, but then there is a gap at the end where the 8th product moves onto the next page.
At first I thought it was CSS widths or something but it's not. Even if I have 20 products...always the last row only shows 3 across and puts the last product on the next page.
Any ideas anyone?
Cheers M
<ul>
<?php $i=count($products); ?>
<li
<?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
Let's try it..

show an additional image if the item in the database was added 3 days ago or less using php

I am trying to display an addition image next to an item if it was added to the database 3 days ago or less saying "new"?
In a table called movie there is a field called moviedateadded which stores when the movie was added to the table in DATE format e.g. 2011-09-01.
I'm guessing I need to get the current date from the server, and use an if else statement to say if the moviedateadded is 3 days or less than the server time, echo out "new.jpg"?
Please confirm the best way to do this?
Thanks in advance.
<?php
$date = "2011-09-01";
if( (time() - strtotime($date)) > 3*24*60*60 ){
echo "More than three days";
}
?>
According to your code (edited also <= for 3 or less instead of more than 3
<?php
while($row = mysql_fetch_array($result)){
if( (time() - strtotime($row['moviedateadded'])) <= 3*24*60*60 ){
echo "<img src='images/twit.png'/>";
}
?>
<a href="movie.php?movieid=<?php echo $row['movieid']?>">
<img src="content/movie/<?php echo $row['movieimage'] ?>.jpg">
</a>
<?php
}
?>

Categories