I was wondering whether you guys could help me figure out why this isn't generating the HTML correctly. The variable $wholeTeam is returned from $wpdb->get_results(...) and is confirmed to be of length 3 (because echo count($wholeTeam) is spitting that number out and I've directly looked in the MySQL db to make sure there are 3 rows). For some reason, only the last of the 3 is being generated; the first 2 aren't.
<h2>Current members <span class="title-count"> <?php echo count($wholeTeam) ?> </span></h2>
<div class="row">
<?php
$sandwichTop = '<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3"><img src="/assets/';
$sandwichBottom = '/><button type="button" class="edit-mem-btn wp-core-ui button-primary">Edit</button></div>';
foreach ($wholeTeam as $thisMember)
$sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
echo $sandwichTop . $sandwichMiddle . $sandwichBottom;
?>
</div>
gets generated as
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<img src="/assets/thepic.png" id="memberid-3"/>
<button type="button" class="edit-mem-btn wp-core-ui button-primary">Edit</button></div>
</div>
</div>
Code
foreach ($wholeTeam as $thisMember)
$sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
echo $sandwichTop . $sandwichMiddle . $sandwichBottom;
is equivalent to:
foreach ($wholeTeam as $thisMember) {
$sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
}
echo $sandwichTop . $sandwichMiddle . $sandwichBottom;
That's why you echo only once. If you want to echo every foreach iteration:
foreach ($wholeTeam as $thisMember) {
$sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
echo $sandwichTop . $sandwichMiddle . $sandwichBottom;
}
Related
I have function that returns html code
And I want to add foreach loop to return code
I try this code but I get an error
return '<section class="n-hero-section-two rebuild" ' . str_replace('\\', "", $bg_img) . '>
...
<div class="n-hero-two-box">
<div class="n-hero-two-main-text">
' . $main_section_tagline .'
' . $main_section_title . '
</div>
<div class="n-hero-two-form-cat">
...
<div id="search-headline" class="panel-collapse collapse in" role="tabpanel">
...
<ul class="live-filter live-filter-nd">'
// I want to add foreach here
foreach (array_combine($allprofessionsLinks, $allprofessions) as $allprofessionsLinks => $allprofessions) {
// echo '<option value="' . $allprofessionsLinks . '">' . $allprofessions . '</option>';
echo"<li>" . $allprofessions . "</li>";
}
'</ul>
</div>
</form>
...
</div>
</div>
</section>';
Add foreach result to string and add string to return
$live_search_result = '';
foreach (array_combine($allprofessionsLinks, $allprofessions) as $allprofessionsLinks => $allprofessions) {
$live_search_result = $live_search_result . "<li>" . $allprofessions . "</li>";
}
And add $live_search_result string to result
<ul class="my-custom-class">'
. $live_search_result .
'</ul>
I want to add CCS style which depends on php IF statement. I have found something: changing the style inside "if" statement, but somehow it doesn`t work, maybe because of WHILE statement in my code.
<?php
$possible_lang_list = mysqli_query($con,
'SELECT * FROM page WHERE title = "lang" ORDER BY name1 ASC');
while ($row = mysqli_fetch_array($possible_lang_list)) {
echo '
<div class="language" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
?>
I want to display only div class="language" where $row['name2'] == $x, with possibility to display whole class "language" with JavaScript. Could anyone help?
I have tried:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
}
now it is working:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php }
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
Try this code :
while ($row = mysqli_fetch_array($possible_lang_list))
{
if($row['name2'] == $x)
{
echo '<div class="language" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
else
{
echo '<div class="" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
}
Hope it will work
Assign a style element with a true/false ternary, like below. Then just output it!
<?php
while ($row = mysqli_fetch_array($possible_lang_list)) {
$class = ($row['name2'] == $x) ? 'language' : null;
echo '
<div class="'.$class.'" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
And then:
I am trying to figure out how to remove the comma from, say the
['address_2'] field if this is left empty and also not show the icons for say the ['tel'] phone number if this is not inputted, It seems hard working within the echo.
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
echo "<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2>" . $row['company_name'] . "</h2>
<p><strong>" . $row['company_services'] . "</strong></p>
</div>
<div class='box'><strong> Address: </strong>" . $row['address_1'] . ", " . $row['address_2'] . ", " . $row['town'] . ", " . $row['county'] . ", " . $row['postcode'] . "</div>
<div class='box'><strong> Telephone: </strong>" . $row['tel'] ."<strong> Mobile: </strong>". $row['mob'] . "</div>
<div class='iconbox'>
<a href='" . $row['tel'] . "'><img src='img/icons/phone.png' class='hvr-push'></a>
<a href='" . $row['mob'] . "'><img src='img/icons/mobile.png' class='hvr-push'></a>
<a href='mailto:" . $row['email'] . "'><img src='img/icons/email.png' class='hvr-push'></a>
<a href='" . $row['web_url'] . "'><img src='img/icons/web.png' class='hvr-push'></a>
</div>
<div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/image1.png'>
</div>
<div class='mapbox'>
" . $row['gmap_url'] . "
</div>
</div>
</div>
<br/>";
}
}
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
?>
<div class='result-container'>
<div class='result-wrap'>
<div class='leftbox'>
<div class='titlebox'>
<h2><?=$row['company_name']?></h2>
<p><strong><?=$row['company_services']?></strong></p>
</div>
<div class='box'><strong> Address: </strong><?=$row['address_1']?>,
<?php
if(!empty($row['address_2'])){
?>
  <?=$row['address_2']?>,
<?php
}
?>
  <?=$row['town']?>,  <?=$row['county']?>,  <?=$row['post code']?></div>
<div class='box'><strong> Telephone: </strong><?=$row['tel']?><strong> Mobile: </strong><?=$row['mob']?></div><div class='iconbox'>
<a href='
<?=$row['tel']?>'><img src='img/icons/phone.png' class='hvr-push'></a>
<a href='
<?=$row['mob']?>'><img src='img/icons/mobile.png' class='hvr-push'></a>
<a href='mailto:"
<?=$row['email']?>'><img src='img/icons/email.png' class='hvr-push'></a>
<a href='"
<?=$row['web_url']?>'><img src='img/icons/web.png' class='hvr-push'></a>
</div>
<div class='buttonwrap'><div class='buttonbox'><p>View Profile</p></div></div>
</div>
<div class='rightbox'>
<div class='service-pic'>
<img src='img/image1.png'>
</div>
<div class='mapbox'>
<?=$row['map_url']?>
</div>
</div>
You can use Mysql, see it here: http://php.net/manual/es/book.mysqli.php
And a final tip: 'Do not enter HTML into a echo'
Ok, this is my first time using Stack so I apologize in advance for anything I do incorrectly.
The Situation:
I have a class project to rewrite HTML code in PHP. Here is a snippet of the HTML code.
<div class="col-small-6 col-med-6 col-lg-4 albumContainer">
<img src="_images/elephant_king_cover_240x240.png" alt="">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Elephant King</a></h2>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>Elephant King</li>
<li>Joy & Sorrow</li>
<li>Traverse</li>
<li>Tres Capos</li>
<li>Timepiece</li>
<li>Adventures in Sawyerland</li>
<li>Be Still</li>
<li>Overtime</li>
<li>Bongolo</li>
<li>Coronation</li>
<li>Anchor</li>
</ol>
<h3>available at:</h3>
iTunes
Amazon
United Interests
</div>
</div>
</div>
</div>
</div><!-- end albumContainer -->
The structure repeats with different content.
The Problem:
When I run my SQl/PHP I am getting all of the information I am asking for, but it is not rendering properly. I get all of my information over and over again for as many different song titles there are (the ordered list).
I want everything to run one time for each section and for the song titles to be the content for the accordion. Here is the SQL/PHP code I have been messing around with.
<?php
require('mysqli_connect_remote.php');
$q = "SELECT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";
$result = mysqli_query($dbcon, $q);
if ($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
// <h3>available at:</h3>
// <a href=' . ['Location'] . '>iTunes</a>
// <a href=' . ['Location2'] . '>Amazon</a>
// <a href=' . ['Location3'] . '>United Interests</a>
// ';}
mysqli_free_result ($result);
} else {
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Any insights on how to make this work for me would be most appreciated.
First, if you're doing loops in loops, or if-statements in loops, or loops in if-statements, indent better to where its clear what's in what. Sounds trivial, but it makes all the difference in the world.
And I would suggest losing the Egyptian brackets. You know, the ones that look like someone walking like an Egyptian. That is, start putting opening brackets on a new line so the opening and closing brackets line up which makes it easy to see when a bracket is missing.
I have re-indented your code without changing anything:
if ($result)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
/*<h3>available at:</h3>
<a href=' . ['Location'] . '>iTunes</a>
<a href=' . ['Location2'] . '>Amazon</a>
<a href=' . ['Location3'] . '>United Interests</a>
';}*/
mysqli_free_result ($result);
}
else
{
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Now you can actually see what's going on. Initially when I re-indented the first time I missed one of your closing brackets that was hiding at the end of a long long line and thought you were missing the closing bracket of the loop.
And it seems the problem is actually with your SQL query. Throwing a distinct in there might solve it:
$q = "SELECT DISTINCT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";
why isn't my while loop getting the mysql database entries and presenting them like demonstrated? Thanks :).
<?php $djs_all_db = mysql_query("SELECT * FROM djs")
or die(mysql_error());
$djs_all_num = mysql_num_rows($djs_all_db);
while($djs_all = mysql_fetch_array( $djs_all_db )) {
if ($djs_all_num % "2") {
echo "<div class=\"row\">
<div class=\"column column-2\">
<img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>
<p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
<p class=\"float-right\">" . $djs['position'] . "</p>
<div class=\"clear\"></div>
<p>" . $djs['biography'] . "</p>
</div>";
} else {
echo "<div class=\"column column-3\">
<img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>
<p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
<p class=\"float-right\">" . $djs['position'] . "</p>
<div class=\"clear\"></div>
<p>" . $djs['biography'] . "</p>
</div></div>";
} }?>
Your loop defines: $djs_all -- In your code you use: $djs['username'], change it to $djs_all['username']
Don't know if that's your issue, but
if ($djs_all_num % "2") {
tests a static number( the number of entries in your table), so you'll always execute the if or the else part.
Also, you loop on a djsall variable:
while($djs_all = mysql_fetch_array( $djs_all_db )) {
But you try to access fields from a $djs one:
<img src=\"images/about/" . $djs['username']