Loop and if Statement in php - php

I have the following code.
<?php if ( $showdata['venue'] == "Brean Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Camber Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Pakefield" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Prestatyn Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Southport" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
This pulls the information from my db just fine, but I need it to loop, or repeat so that multiple dates appear for each venue. There are multiple dates for each venue in the db, but as the code is, it only returns the value for the first date. I need the others to appear as well.
How do I get this code to loop while looking at only one venue and pull all dates, then end?

$query = mysql_query("the query");
while($showdata = mysql_fetch_assoc($query)) {
put your code here
}
So you run the query to get all database records you want, then you loop through assigning the row to $showdata. This will then process everything inside the loop for every row it returns from the database query

assuming you use mysql_ function to fetch values from db. here is what you should do.
$query = mysql_query('SELECT QUERY');
while($showdata = mysql_fetch_assoc($query)) {
echo "<div class=\"gigpress-related-park\">" . $showdata['venue'] . $showdata['venue'] . "</div>";
}
however it would be better of using PDO or MySQLi instead of using mysql_.

Related

How To Parts Of Speech Only One Time In PHP

I want to show only one time the heading of parts of speeches for example when user enter the word "Spell" all the parts of speeches and its meaning come every time .
The headings are repeating multiple times . i want to print only one time
There should be a heading Of Noun,Verb,Adjective etc..
like this
Noun.
Verb.
adjective.
this is my code
while($row=mysql_fetch_array($sql)){
echo "<div id='wordid' style='display:none'>".$row["wordid"]."</div>";
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
You can keep track of what the last wordid was and only echo the header if it changed.
// initialize to avoid notices
$lastWordId = '';
while($row=mysql_fetch_array($sql)){
if ($lastWordId != $row['wordid']) {
echo "<div id='wordid' style='display:none'>".
$row["wordid"].
"</div>";
$lastWordId = $row['wordid'];
}
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}

How to assign a PHP expression to a PHP variable

I need to do something like
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
Any help will be highly useful. I am stuck in this from the very long time but not able to acheive this. please help
Below is my PHP code that I need to assigned again to php variable (this variable will be passed to return of my ajax call)
<div class="photo-post">
<h4 class="h4-1">Some data...</h4>
<?php
$i=0;
$unique_seq="888094499";
$pic_counter=1;
foreach (glob("target/*".$unique_seq."*") as $filename)
{
$i++;
}
$total_pics=$i;
echo "<div id='post-photo-slider'>";
if($i>1)
{
echo "<a href='#' class='control_next_photo'>></a>";
echo "<a href='#' class='control_prev_photo'><</a>";
}
echo "<ul>";
foreach (glob("target/*".$unique_seq."*") as $filename)
{
echo "<li>";
echo "<div class='photos-slide-container'>";
echo "<img src='".$filename."' width='540px' height='225px'>";
echo "<span class='count-span-photos'>".$pic_counter." of ".$total_pics."</span>";
echo "</div>";
echo "</li>";
$pic_counter++;
}
if ($i==1)
{
echo "<li></li>";
}
echo "</ul>";
echo "</div><!-- post-photo-slider END -->";
echo "<hr class='hr4'>";
?>
</div><!-- photo end -->
you cannot do it in this way
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
when you return this string to your javascript php code cannot be execute on client side!!!!
why do not return do this simple way?
$var = "Hello World ..the value is " . "XYZ";

I want to add .html at the end or a full url fetch from Database and php query between echo

I am going to write following code for my blog. But problem, I am facing with:
"echo 'a href=".$query2['url']."</a>';" I want to add .$query2['url']. in echo and also want to add .html at the end of url like
"a href=".$query2['url'].html.".
If you have any idea please tell me as soon as possible thanks. Basically I am fetching this data from database .
<?php
$start=0;
$limit=5;
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
$query=mysql_query("select * from tbl_services LIMIT $start, $limit");
echo "<div class='entry'>";
while($query2=mysql_fetch_array($query))
{
echo '<h2>' .$query2['name'].'</h2>';
echo "<p>".$query2['Contents']."</p>";
echo "<div class='meta'><i class='fa fa-clock-o'></i>".$query2['date']. "<b><i class='fa fa-user'></i></b> Written by <strong>Arslan Ali</strong> <b><i class='fa fa-comment-o'></i></b>" ;
echo "</div>";
}
echo "</div>";
$rows=mysql_num_rows(mysql_query("select * from tbl_services"));
$total=ceil($rows/$limit);
echo "<ul class='pagination'>";
if($id>1)
{
echo "<li><a href='?id=".($id-1)."' class='disabled'><<</a></li>";
}
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
if($id!=$total)
{
echo "<li><a href='?id=".($id+1)."' class='disabled'>>></a></li>";
}
echo "</ul>";
?>
(Updated)
Replace this line
echo '<h2>' .$query2['name'].'</h2>';
with
echo "<a href='".$query2['url'].".html'>".$query2['name']."</a>";
There is still one problem with the solution made by Syntax Error. The href is in single quotes. This is not the proper Syntax. It is easily remedied by reversing the use of the single and double quotes as I have shown here.
echo '' . $query2['name']. '';

Add class to variable inside echo

How can i add to variable $title inside echo ? In the foreach:
<?php
foreach($this->searchfields as $fsearch) {
$title = $this->field->showFieldTitle($this->catid,$fsearch);
echo "<div id=".$fsearch->name.">".htmlspecialchars($title)."";
$this->field->showFieldSearch($fsearch,$this->catid,null);
echo "</div>";
}?>
I tried echo "<div id=".$fsearch->name.">".htmlspecialchars(<div class="header">$title</div>).""; but it doesnt work :)
How about:
echo "<div id='".$fsearch->name."'><div class='header'>".htmlspecialchars($title)."</div>";
?

Need help showing code results in a table

I'm trying to get this
for ($r = 1; $r <= 10; $r++) {
if ($shirt_info[$r][0] != "Select One")
{
echo "<div class='shirtinfo'>Order $r: </div>";
echo "<div class='shirtinfo'>Size: ".$shirt_info[$r][0]."</div>";
echo "<div class='shirtinfo'>Price: ".$shirt_info[$r][1]."</div>";
echo "<div class='shirtinfo'>Qty: ".$shirt_info[$r][2]."</div>";
echo "<div class='shirtinfo'>SPE: ".$shirt_info[$r][5]."</div>";
echo "<div class='shirtinfo'>LIA: ".$shirt_info[$r][6]."</div>";
echo "<div class='shirtinfo'>Vendor: ".$shirt_info[$r][7]."</div>";
echo "<div class='shirtinfo'>Style: ".$shirt_info[$r][8]."</div>";
echo "<div class='shirtinfo'>Color: ".$shirt_info[$r][9]."</div>";
echo "<div style='clear:both;'></div>";
}
}
to show in a table and I'm not really sure how. I keep getting a syntax error.
The code you are showing us has an unterminated for loop. Maybe this is the syntax error.
Your for does not end with }
I would also suggest that you use table and not div if you want to display a table.

Categories