I need to change the div's class name based on if condition below it.
i have tried as <div class="<?php echo $class_name; ?>">
I want the class name to be "class name 1" or "class name 2" based on a if condition like if submitted and correct answer are same, i want the class name to be class_name1 or else it should be class_name2. The code is as follows:
<element class="col-sm-6 col-aligncenter">
<!--<div class="pipeline white lined-success"> -->
<div class="<?php echo $class_name; ?>">
<div class="pipeline-header">
<h5>
<b><?php echo $count++;?>. <?php echo $question_type == 'fill_in_the_blanks' ? str_replace('^', '__________', $question_title) : $question_title;?></b>
</h5>
<span><?php echo get_phrase('mark');?>: <?php echo $mark;?></span>
</div>
<?php if ($question_type == 'multiple_choice'):
$options_json = $this->crud_model->get_question_details_by_id($row['question_bank_id'], 'options');
$number_of_options = $this->crud_model->get_question_details_by_id($row['question_bank_id'], 'number_of_options');
if($options_json != '' || $options_json != null)
$options = json_decode($options_json);
else $options = array();
?>
<ul>
<?php for ($i = 0; $i < $number_of_options; $i++): ?>
<li><?php echo $options[$i];?></li>
<?php endfor; ?>
</ul>
<?php
if ($row['submitted_answer'] != "" || $row['submitted_answer'] != null)
{
$submitted_answer = json_decode($row['submitted_answer']);
$r = '';
for ($i = 0; $i < count($submitted_answer); $i++)
{
$x = $submitted_answer[$i];
$r .= $options[$x-1].',';
}
} else {
$submitted_answer = array();
$r = get_phrase('no_reply');
}
?>
<i><strong>[<?php echo get_phrase('answer');?> - <?php echo rtrim(trim($r), ',');?>]</strong></i>
<br>
<?php
if ($row['correct_answers'] != "" || $row['correct_answers'] != null) {
$correct_options = json_decode($row['correct_answers']);
$r = '';
for ($i = 0; $i < count($correct_options); $i++) {
$x = $correct_options[$i];
$r .= $options[$x-1].',';
}
} else {
$correct_options = array();
$r = get_phrase('none_of_them.');
}
?>
<i><strong>[<?php echo get_phrase('correct_answer');?> - <?php echo rtrim(trim($r), ',');?>]</strong></i>
<?php elseif($question_type == 'true_false'):
if ($row['submitted_answer'] != "") {
$submitted_answer = get_phrase($row['submitted_answer']);
}
else{
$submitted_answer = get_phrase('no_reply');
}
?>
<i><strong>[<?php echo get_phrase('answer');?> - <?php echo get_phrase($submitted_answer);?>]</strong></i><br>
<i><strong>[<?php echo get_phrase('correct_answer');?> - <?php echo get_phrase($row['correct_answers']);?>]</strong></i>
**div's class should change based on if answer and correct answer are equal**
There are two types of questions (multiple choice and true or false). It should behave same for both type of questions.
Related
I have set-up a news page which retrieves news from MYSQL news table.
I am trying to identify if the news column is odd or even, so if the news columns is odd or even it will add a class to the div element.
My code is as follows:
<?php
$cat = $_GET['cat'];
$date = $_GET['date'];
if ($date !="") {
$date = explode('-', $date);
$year = $date[1];
$month = $date[0];
$month = date("m", strtotime($month));
$sql = "SELECT * FROM news WHERE year(newsDate) = '$year' AND month(newsDate) = '$month' AND newsState = 1 ORDER BY newsDate DESC";
} else {
$sql = "SELECT * FROM news WHERE newsState = 1 ORDER BY newsDate DESC";
}
$result = $conn->query($sql);
$rows = $result->num_rows;
$pager = new PS_Pagination($conn, $sql, 5, 10, null);
$rs = $pager->paginate();
$num = $rs->num_rows;
if($num >= 1 ){
while($row = $rs->fetch_assoc()){
?>
<div class="news <?php echo $num; ?>">
<div class="four columns">
<p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
</div>
<div class="eight columns">
<h3><?php echo $row['newsTitle']; ?></h3>
<p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
</div>
</div>
<?php } } else {
echo "No records found!";
}
if ($rows >= 5) {
echo "<div class='page-nav blog-nav'>";
echo $pager->renderFullNav();
echo "</div>";
}
?>
TAke any flag which maintains odd-even position...
$f = 0; //ADDED THIS LINE
if($num >= 1 ){
while($row = $rs->fetch_assoc()){
if($f%2==0) //ADDED THIS LINE
$class_name = "even"; //ADDED THIS LINE
else //ADDED THIS LINE
$class_name = "odd"; //ADDED THIS LINE
?>
<div class="news <?php echo $class_name; ?>">
<div class="four columns">
<p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
</div>
<div class="eight columns">
<h3><?php echo $row['newsTitle']; ?></h3>
<p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
</div>
</div>
<?php $f++; } } else {
echo "No records found!";
}
if($num >= 1 ){
$tr = 1;
while($row = $rs->fetch_assoc()){
if($tr%2 == 0)
{
//then even class
}
else
{
//odd class
}
?>
<div class="news <?php echo $num; ?>">
<div class="four columns">
<p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
</div>
<div class="eight columns">
<h3><?php echo $row['newsTitle']; ?></h3>
<p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
</div>
</div>
<?php $tr++; } } else {
echo "No records found!";
}
Use counter variable and check if it is even or odd ?
Create variable, increment that in each loop and check, if $i % 2 is 0 (even) or 1 (odd).
$i = 1;
while($row = $rs->fetch_assoc()){
?>
<div class="news <?php echo $num; echo $i % 2 == 0 ? ' even' : ' odd' ?>">
<div class="four columns">
<p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
</div>
<div class="eight columns">
<h3><?php echo $row['newsTitle']; ?></h3>
<p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
</div>
</div>
<?php
$i++;
}
Take $classname = '';
Get news ID in loop and divide / 2 and compare whether you getting odd / even value. In case odd value add $classname = 'oddCLASS' and in case even value add $classname = 'evenCLASS' and updare class = $classname wherever you required.
This way it will automatically update dynamic class.
so the fastest way to do this is just by using a boolean:
$odd = false;
while ( .... )
{
echo ($odd = !$odd) ? 'odd' : 'even';
}
No need to keep a counter for that, no need for a modulo and it keeps the code pretty clean. My preferred way unless you need to keep a counter. And even then: $counter & 1 == 1 is faster than $counter % 2 == 1 and does exactly the same.
Simple explanation:
echo ($odd = !$odd) ? 'odd' : 'even';
// will become
$odd = ! $odd; // it flips the boolean
if ($odd)
echo 'odd';
else
echo 'even';
I'm building a site that uses a video plugin to generate a gallery of links to videos. The problem is that these images and links are put into a table, which is incompatible with the responsive design of my site. Being a web designer and not a PHP-developer, I'm at a loss when looking at this code about how to take out all the elements that generate the table and replace them with simple div containers, though it looks like it should be pretty straightforward for a PHP person.
<table id="media_galery">
<?php
$t = 0;
for ($i = 0; $i < count($cArray); $i++ ) {
$t++;
$file = $cArray[$i];
$remote = null;
$name = $file->getFileName();
$file_path = BASE_URL.DIR_REL.$file->getDownloadURL();
$ak_d = FileAttributeKey::getByHandle('media_date');
$date = $file->getAttribute($ak_d);
$ak_a = FileAttributeKey::getByHandle('media_artwork');
$image = $file->getAttribute($ak_a);
if($image==null){
$image_path=$uh->getBlockTypeAssetsURL($bt).'/tools/mp3.png';
}else{
$image = File::getByID($image->fID);
$image_path=$image->getURL();
}
$file_src = BASE_URL.DIR_REL.$file->getRelativePath();
$ak_s = FileAttributeKey::getByHandle('media_audio');
$audio = $file->getAttribute($ak_s);
if($audio){
$audio_array = $mh->getMediaTypeOutput($audio);
$audio_path = $audio_array['path'];
$file_src = $audio_path;
}
$video = null;
$ak_s = FileAttributeKey::getByHandle('media_video');
$video = $file->getAttribute($ak_s);
if($video){
$video_array = $mh->getMediaTypeOutput($video);
$video_path = $video_array['path'];
var_dump($video_array['id']);
$image_path = $mh->getMediaThumbnail($video_array['id'],$video_array['type']);
//$video_src = $video_path.'?width='.$width.'&height='.$height;
//$file_src = $video_src;
if($video_array['type'] == 'vimeo'){
$file_src = 'http://player.vimeo.com/video/'.$video_array['id'];
}else{
$file_src = $video_path;
}
}
?>
<td valign="top">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
</td>
<?php
if($t == $spread){
$t=0;
echo '</tr>';
}
}
for($d=$t;$d<$spread;$d++){
echo '<td></td>';
if($t == $spread){
echo '</tr>';
}
}
?>
</table>
Any help on this would be very much appreciated!!
<div id="media_galery">
<?php
$t = 0;
for ($i = 0; $i < count($cArray); $i++ ) {
$t++;
$file = $cArray[$i];
$remote = null;
$name = $file->getFileName();
$file_path = BASE_URL.DIR_REL.$file->getDownloadURL();
$ak_d = FileAttributeKey::getByHandle('media_date');
$date = $file->getAttribute($ak_d);
$ak_a = FileAttributeKey::getByHandle('media_artwork');
$image = $file->getAttribute($ak_a);
if($image==null){
$image_path=$uh->getBlockTypeAssetsURL($bt).'/tools/mp3.png';
}else{
$image = File::getByID($image->fID);
$image_path=$image->getURL();
}
$file_src = BASE_URL.DIR_REL.$file->getRelativePath();
$ak_s = FileAttributeKey::getByHandle('media_audio');
$audio = $file->getAttribute($ak_s);
if($audio){
$audio_array = $mh->getMediaTypeOutput($audio);
$audio_path = $audio_array['path'];
$file_src = $audio_path;
}
$video = null;
$ak_s = FileAttributeKey::getByHandle('media_video');
$video = $file->getAttribute($ak_s);
if($video){
$video_array = $mh->getMediaTypeOutput($video);
$video_path = $video_array['path'];
var_dump($video_array['id']);
$image_path = $mh->getMediaThumbnail($video_array['id'],$video_array['type']);
//$video_src = $video_path.'?width='.$width.'&height='.$height;
//$file_src = $video_src;
if($video_array['type'] == 'vimeo'){
$file_src = 'http://player.vimeo.com/video/'.$video_array['id'];
}else{
$file_src = $video_path;
}
}
?>
<div class="img_div<?php if($t == ($spread + 1)){$t=0; echo ' first';}?>">
</a><img src="<?php echo $image_path?>" href="#popup_prep" alt="<?php echo $file_src?>" class="<?php echo $playerID?>player_get<?php echo $i?> gallery_thumb" href="<?php echo $file_src?>"/>
</div>
<?php
}
?>
</div>
I don't know what the value of $spread is, but with the code above you will output something like this:
<div id="meda_galery">
<div class="img_div">
...
<div>
<div class="img_div">
...
<div>
<div class="img_div first">
...
<div>
<div class="img_div">
...
<div>
</div>
The example above assumes a spread of 2 (so 2 columns). You could float each div left but use clear to start a new row for each one with a class of "first".
<? if (isset($menu_filter_sizes)) { ?>
<? for ($i = 0; $i < count($menu_filter_sizes); $i++) { ?>
<? if (!empty($menu_filter_sizes[$i]['name'])) { ?>
<li>
<? if ($page->sp_size == $menu_filter_sizes[$i]['id']) { ?>
<? } else { ?><a href=""><? }; ?>
<? echo $menu_filter_sizes[$i]['name']; ?>
<? if ($page->sp_size == $menu_filter_sizes[$i]['id']) { ?>
<? } else { ?></a><? }; ?>
(<? echo $menu_filter_sizes[$i]['quantity']; ?>)
</li>
<? }; ?>
<? }; ?>
<? }; ?>
This code generates a list with different values (e.g. Sizes). And i have in my DB a value which is called "Nosize". This is value for products with no size. Present PHP code that i have it outputs all the values in the list.
HOW with php to hide that value by the name "Nosize"??? and show the others values only. in another words, show all values, except "Nosize".
MYSQL:
function GetShopFilterSizes($shopid,$category,$fcolor,$fbrand,$fytpe,$fpricefrom,$fpriceto){
$thisdate = date("Y-m-d H:i:s", time());
$sqlquery = "SELECT DISTINCT($this->basesq.size) AS id, $this->basesize.alias, $this->basesizelang.name, COUNT(DISTINCT($this->base.id)) as quantity FROM $this->base LEFT JOIN $this->basesq ON $this->basesq.productid = $this->base.id LEFT JOIN $this->basesize ON $this->basesq.size = $this->basesize.id LEFT JOIN $this->basesizelang ON $this->basesize.id = $this->basesizelang.sizeid AND $this->basesizelang.lang = '".sql($this->thislang)."' WHERE $this->base.shopid = '".Sql($shopid)."' AND $this->base.exist = 1 AND $this->base.active = 1 AND $this->base.isdeleted = 0";
if (count($category) > 0) {$sqlquery .= " AND (";};
for ($i = 0; $i < count($category); $i++) {
if ($i == 0) {$sqlquery .= "$this->base.categoryid = '".sql($category[$i])."'";}
else {$sqlquery .= " OR $this->base.categoryid = '".sql($category[$i])."'";};
};
if (count($category) > 0) {$sqlquery .= ")";};
if (!empty($fbrand)) {$sqlquery .= " AND $this->base.brand = '".sql($fbrand)."'";};
if (!empty($ftype)) {$sqlquery .= " AND $this->base.type = '".sql($ftype)."'";};
if (!empty($fcolor)) {$sqlquery .= " AND $this->basesq.color = '".sql($fcolor)."' AND $this->basesp.quantity > '0'";};
if (!empty($fpricefrom)) {$sqlquery .= " AND ($this->base.price >= '".sql($fpricefrom)."')";};
if (!empty($fpriceto)) {
$sqlquery .= " AND (
($this->base.price <= '".sql($fpriceto)."') OR ($this->base.specialprice IS NOT NULL AND $this->base.specialprice < '".sql($fpriceto)."') OR ($this->base.salesprice IS NOT NULL AND $this->base.salesprice < '".sql($fpriceto)."' AND (($this->base.slprice_datestart IS NULL OR $this->base.slprice_datestart < '".$thisdate."') AND ($this->base.slprice_dateend IS NULL OR $this->base.slprice_dateend > '".$thisdate."')))
)";
};
$sqlquery .= " GROUP BY $this->basesq.size";
$result = mysql_query($sqlquery) or die(mysql_error()." in file: ".__FILE__.", line: ".__LINE__);
return GetMySQLResultArray($result);
}
the whole HTML text
<div class="box manufacturers">
<div class="box-heading" style="border-bottom: 1px solid #d8dae5;"><span><? echo $langdata['prlist_size']; ?></span></div>
<? if (isset($menu_filter_sizes)) { ?>
<div class="box-content">
<ul>
<? for ($i = 0; $i < count($menu_filter_sizes); $i++) { ?>
<? if($menu_filter_sizes[$i]['name']!='nosize') { ?>
<li>
<? if ($page->sp_size == $menu_filter_sizes[$i]['id']) { ?><u>
<? } else { ?><a href="<? echo ClearURL(array('success','size'),array('_size='.$menu_filter_sizes[$i]['alias'])); ?>"><? }; ?>
<? echo $menu_filter_sizes[$i]['name']; ?>
<? if ($page->sp_size == $menu_filter_sizes[$i]['id']) { ?></u>
<? } else { ?></a><? }; ?>
</strong> (<? echo $menu_filter_sizes[$i]['quantity']; ?>)
</li>
<?}else{?>
<? }; ?>
<? }; ?>
</ul>
</div>
<? }; ?>
</div>
<? if (isset($menu_filter_sizes)) {
for ($i = 0; $i < count($menu_filter_sizes); $i++) {
if($menu_filter_sizes[$i]['size']!='Nosize') {//check if size is not equal to Nosize
}
}
} ?>
Also you are opening and closing php tags at every line which is not needed.
Update: From the OP comments
you need to take your code outside div
<? if (isset($menu_filter_sizes)) {
for ($i = 0; $i < count($menu_filter_sizes); $i++) {
if($menu_filter_sizes[$i]['size']!='Nosize') {//check if size is not equal to Nosize
?>
<div class="box manufacturers">
</div>
<?
}
}
}
?>
I have few drop down boxes from where I can get an id of a category. For example, from drop down box 1, i get $cat1, from box 2 i get $cat2 and so on.
Then I want to get the entries from db for each of the cat id. Currently I am repeating same code for each of the variable, like:
<?
$cat1 = 1;
$cat2 = 3;
$cat3 = 4;
$cat4 = 8;
<? if ($var1 != ""){ ?>
<div>
Entries for <? echo $var1; ?>
..
</div>
<? } ?>
<? if ($var2 != ""){ ?>
<div>
Entries for <? echo $var2; ?>
..
</div>
<? } ?>
<? if ($var3 != ""){ ?>
<div>
Entries for <? echo $var3; ?>
..
</div>
<? } ?>
I'd like to know if I can use a loop and avoid writing code for each variable.
Try
<?
$cats = array(1,3,4,8);
foreach($cats as $value) {
if($value != "") {
?>
<div>Entries for <?= $value; ?></div>
<?
}
}
?>
Use an array like this:
$cat[1] = 'bla';
$cat[2] = 'Bla2';
foreach ($cat as $c){
if ($c != ""){
echo '
<div>
Entries for '. $c.'
..
</div>';
}
}
I have this php foreach loop
<?php $all_news = $this->db->query("Select * from news"); ?>
<div class="inner">
foreach($all_news as $key=>$news){?>
<div class="news <?php echo ($key%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
But the problem is the $all_news may have 20 results or so but the design only allows me to put for 4 news blocks in each inner div...is there a way make this happen so i have only 4 news divs in each inner div
<?php
$all_news = $this->db->query("Select * from news");
echo '<div class="inner">';
$c = count($all_news);
for($i = 0; $i < $c; $i++){
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
if($i % 4 == 3)
echo '</div><div class="inner">';
}
echo '</div>';
?>
Change your query to only return 4 rows:
SELECT * FROM news LIMIT 4
Alternatively you can change your for-loop.
for($i = 0; $i < min(4, count($all_news)); $i++)
{?>
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$i]['title'];
<?}
[edit]
See what you mean now. Create two loops:
<?
$index = 0;
while ($index < count($all_news))
{
$news = $all_news[$index];
?>Start outer div<?
for ($item = 0; $item < 5; $item++)
{
?>Inner div with news item <? echo $news['title'];
}
?>End outer div<?
$index++;
}
You could use two for loops:
<?php $all_news = $this->db->query("Select * from news"); ?>
<?php for($i = 0, $l = count($all_news); $i < $l; $i+=4): ?>
<div class="inner">
<?php for($j = $i; $j < $i+4; $j++): ?>
<div class="news <?php echo ($j%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$j]['title'] ?>
<?php endfor;?>
</div>
<?php endfor;?>
Another option would be array_chunk [docs].
The laziest way would be to just check whether or not you've already done four in the current div on the fly. If you have, close the current div and start a new one:
<div class="inner">
<?php
foreach ($all_news as $key => $news) {
if ($key % 2) {
$oddEven = 'odd';
} else {
$oddEven = 'even';
if ($key && $key % 4 === 0) {
echo '</div><div class="inner">';
}
}
echo "<div class=\"news $oddEven\">";
// ...
}
?>
</div>
Note that this assumes $all_news has an element at 0, so it makes sure that it doesn't close the first, empty div.