I have the following variable $prod_list_contents which is a mix of html and php METHODS to display the products from my database:
$prod_list_contents .= '<div style="width:100%;">';
$prod_list_contents .= '';
while ($listing = tep_db_fetch_array($listing_query)) {
$rows++;
$prod_list_contents .= '
<div class="cat_prod_box">
<div class="image">
<div class="cat_image_table">
<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES_CAT . $listing['products_image'], $listing['products_name'], 255, 340, 'class="cat_image_round"') . '
<div class="hidden_thing">Click to View</div>
</a>
</div>
</div>
<div class="cat_product_info" >';
$prod_list_contents .= '<div class="span_style_num">Style: '. $listing['products_model'] . '</div>';
$prod_list_contents .= '<div class="span_colors">Colors: red, blue</div>';
$prod_list_contents .= '<div class="span_product_name">' . $listing['products_name'] . '</div>';
$prod_list_contents .= '<div class="span_price">CAD ' .$currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) .'</div>' ;
$prod_list_contents .= '</div></div>';
}
$prod_list_contents .= ' </div>' .
' '
;
echo $prod_list_contents;
I am trying to incorporate a color table into the same div that is being displayed through this same variable. My color table is derived through the following bit of php:
<div >
<?php
$ctr = 0;
$clr=1;
foreach($products_options_array as $products_options_array2) {
$ctr++;
//if it is a color image, or a color hex
if($products_options_array2['color_image']!='')
{
$clr_sec = "style=\"background-image:url(images/pattern/".$products_options_array2['color_image'].") !important; height:28px; width:28px;\"" . "class=\"testy\"";
}
else {
$clr_sec = "style=\"background-color:".$products_options_array2['color_code']." !important; height:28px; width:28px;float:left;\"" . "class=\"testy\"";
}
?>
<div <?php echo $clr_sec;?>> </div>
<?php
$clr++;
} ?>
</div>
I have tried adding it into the prod_list_contents variable but I don't think I can because of the php here is not METHODS. and requires semi colons etc. Is there another way to go about this? Or am I able to add it in and I'm just being stupid?
If i understand your question correctly you wan to add $clr_sec to $prod_list_contents.
Just change
<div <?php echo $clr_sec;?>> </div>
to
$prod_list_contents.='<div '.$clr_sec.'</div>';
Related
I am having an issue in incrementing the class="accordion-collapse collapse show so that the first accordion tab should open and the closed. This is how I have doneclass="accordion-collapse collapse '.if($i++){.'show'.}.'" . I am do it in a while loop.
if ( $the_query->have_posts() ) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading'.get_the_ID().'">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse'.get_the_ID().'" aria-expanded="true" aria-controls="panelsStayOpen-collapse'.get_the_ID().'">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse'.get_the_ID().'" class="accordion-collapse collapse '.($i++.'show'.).'" aria-labelledby="panelsStayOpen-heading'.get_the_ID().'">
<div class="accordion-body">
'
.the_content() . '
</div>
</div>';
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
I am expecting the first according to open and the rest closed
Try this :-
EDITED.
<?php
$flag = true;
if ($the_query->have_posts()) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ($the_query->have_posts()) {
$the_query->the_post();
$show_class = $flag == true ? 'show' : '';
$collapsed = $flag == true ? '' : 'collapsed';
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading' . get_the_ID() . '">
<button class="accordion-button '.$collapsed.'" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse' . get_the_ID() . '" aria-expanded="true" aria-controls="panelsStayOpen-collapse' . get_the_ID() . '">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse' . get_the_ID() . '" class="accordion-collapse collapse ' . $show_class . ' " aria-labelledby="panelsStayOpen-heading' . get_the_ID() . '">
<div class="accordion-body">
'
. the_content() . '
</div>
</div>';
$flag = false;
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
In my php class i get a result from query and would like to display the data on 2 columns bases on the value from the db table(active or inactive). I would like to have something like this:
active inactive
supplier name a supplier name d
supplier name b supplier name e
supplier name c supplier name f
This is my code:
while ($sensor_row = $result_of_query->fetch_assoc()) {
if ($sensor_row['active'] == 0) {
$status = 's_on';
$thisSensor = '<div class="statusTitle">Active Suppliers</div>';
$thisSensor .= '<div class="row clearfix sensor ' . $sensor_row['id'] . '">';
$thisSensor .= ' <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">';
$thisSensor .= ' <div class="card">';
$thisSensor .= ' <div class="body ' . $status . '"> </div>';
$thisSensor .= ' </div>';
$thisSensor .= ' </div>';
$thisSensor .= '</div>';
echo $thisSensor;
} else if ($sensor_row['active'] == 1) {
$status = 's_on';
$thisSensor = '<div class="statusTitle">Inactive Suppliers</div>';
$thisSensor .= '<div class="row clearfix sensor ' . $sensor_row['id'] . '">';
$thisSensor .= ' <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">';
$thisSensor .= ' <div class="card">';
$thisSensor .= ' <div class="body ' . $status . '"> </div>';
$thisSensor .= ' </div>';
$thisSensor .= ' </div>';
$thisSensor .= '</div>';
echo $thisSensor;
}
}
I have this working code in my view:
<?php
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo '<div class="task">';
echo '<span class="task_id">'.($task_num+1).'.'.'</span>';
echo '<div class="task_time">';
echo '<span class="task_time_start">'.$task->getStartTime().'</span>';
echo '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
echo '</div>';
echo ''.$task->name.'';
echo 'Start';
echo 'Finish';
echo '<div class="status_round '.$task->status.'"></div>';
echo '</div>';
$task_num++;
}
?>
Is there any way to get rid of 'echo'?
P.S. and is the way to insert HTML with HTML helper more correct even if it takes more space?
You can drop echo completely and use native HTML syntax:
<?php $task_num = 0 ?>
<?php foreach ($curent_day->getTasksList() as $task): ?>
<div class='task'>
<span class='task_id'><?= ++$task_num ?></span>
<div class='task_time'>
<span class='task_time_start'><?= $task->getStartTime() ?></span>
<span class='task_time_finish'><?= $task->getFinishTime() ?></span>
</div>
<a href='/' class='task_name'><?= $task->name ?></a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round <?= $task->status ?>'></div>
</div>
<?php endforeach ?>
This will give you better syntax highlighting and autoformatting support in your IDE/editor.
There are several different ways that you can concatenate the string by using single quotes and double quotes, or by using a variable and the ".=" operator to append text onto the end of a string. Just google php strings & concatenation and it will have more than you need to figure this out.
But using you example here is a method:
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo
'<div class="task">' .
'<span class="task_id">' . ($task_num+1) . '.'.'</span>' .
'<div class="task_time">' .
'<span class="task_time_start">' . $task->getStartTime() . '</span>' .
'<span class="task_time_finish">' . $task->getFinishTime() . '</span>' .
'</div>' .
''.$task->name.'' .
'Start' .
'Finish' .
'<div class="status_round '.$task->status.'"></div>' .
'</div>';
$task_num++;
}
You have to echo to output you data to the browser.
You need not use string concatenation or multiple echo statements.
Alternative
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
$task_num++;
echo
"<div class='task'>
<span class='task_id'>{$task_num}</span>
<div class='task_time'>
<span class='task_time_start'>{$task->getStartTime()}</span>
<span class='task_time_finish'>{$task->getFinishTime()}</span>
</div>
<a href='/' class='task_name'>{$task->name}</a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round {$task->status}'></div>
</div>";
}
You can concatenate your html in one variable and then you can use it with one time echo statement
<?php
$task_num = 0;
$html = '';
foreach ($curent_day->getTasksList() as $task){
$html .= '<div class="task">';
$html .= '<span class="task_id">'.($task_num+1).'.'.'</span>';
$html .= '<div class="task_time">';
$html .= '<span class="task_time_start">'.$task->getStartTime().'</span>';
$html .= '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
$html .= '</div>';
$html .= ''.$task->name.'';
$html .= 'Start';
$html .= 'Finish';
$html .= '<div class="status_round '.$task->status.'"></div>';
$html .= '</div>';
$task_num++;
}?>
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 have a script that displays images from a database.
Every 4 pictures is contained in a <div class="row">.
I added a pagination script to this page to limit the number of images and I ran into a problem.
When there are 4 or 8 images on a page the script works fine, but if there are only 1 or 7 images on the page, the closing </div> for the <div class="row"> doesn't get added.
This is my entire script:
$conn = getConnected("lucycypher");
$img_start=0;
$img_limit=8;
if(isset($_GET['page'])) {
$page=$_GET['page'];
$img_start=($page-1)*$img_limit;
}
else { $page = 1; }
$img_total=mysqli_num_rows(mysqli_query($conn, "select * from gallery_img"));
$img_total_count=ceil($img_total/$img_limit);
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
?>
</div>
</div>
<?php
if($_COOKIE['age_verification'] == "adult") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img order by img_time desc LIMIT $img_start, $img_limit;";
}
else if($_COOKIE['age_verification'] == "child") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img WHERE img_category NOT LIKE '%nude' order by img_time desc LIMIT $img_start, $img_limit;";
}
$img_result = mysqli_query($conn, $img_query);
if (mysqli_num_rows($img_result) > 0) {
// output data of each row
$img_count = 1;
while($img_row = mysqli_fetch_assoc($img_result)) {
$tags = $img_row["img_category"];
if ( $img_count%4 === 1 ) { echo '<div class="row">' . PHP_EOL; } // Create new row for every 4th image
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-default">' . PHP_EOL;
echo '<img src="http://lucycypher.com/h/400/w/300/a/c/thumb/'.$img_row["img_location"].'" class="img-responsive img-protected">' . PHP_EOL;
echo '<div class="panel-footer"><span class="glyphicon glyphicon-tag"></span> Tags: ' . $tags . '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row
$img_count++;
}
} else {
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-primary">' . PHP_EOL;
echo '<div class="panel-heading"><span class="glyphicon glyphicon-picture"></span> Sorry</div>' . PHP_EOL;
echo 'No recent uploads.' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
}
?>
<div class="row">
<div class="col-md-12">
<?php
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
Once I figure this out I'm going to limit the images to 8 per page so on the last page if there are only 3 total images then it's missing the final </div> tag since there isn't a total of 4 which completely throws off the rest of the page layout.
Best way to explain it is a visual I assume:
When images total 4 per row with a limit of 8:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
But if the final page only has 3 images:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<!--Missing </div> Tag-->
The final </div> tag is set by this line: if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row.
You may calculate the total count of images, which will be uploaded.
Then, use if ( $img_count%4 === 0 || $img_count==$total_count )
In my example $img_count is the number of added image (beginning from 1)