Thanks in advance.
I am trying to display the name of the current category which I'm in. Nothing that I've been trying for 4 hours now has worked..
My code:
if($catList) {
$c = 0;
while($category = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($category['id'] == $thiscategory) {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
} else {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
what I've tried to get the current category name, in a different area in the page:
if($category['id'] == $thiscategory) {
echo ".strip_tags($category[name])." ;
}
which gives a blank result.
You need:
Compare with $category (instead of $thiscategory), because you set in link &category=
You need to show some difference if value is selected
if($catList) {
$c = 0;
while($myCategory = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($myCategory['id'] == $category) {
echo "<a href='".$link."&category=".$myCategory['id']."' class=\"active\">
<div class='category_name'><strong>".strip_tags($myCategory['name']). "</strong></div></a></div>";
} else {
echo "<a href='".$link."&category=".$myCategory['id']."'>
<div class='category_name'>".strip_tags($myCategory['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
Hey you may try single_cat_title()
Source: https://developer.wordpress.org/reference/functions/single_cat_title/
For a quiz i am Fetching some random questions from a table and displaying the questions (one question at a time) on the webpage.
Now by taking count of the questions fetched i am displaying the numbers starting from 1 as a tab.
i.e. suppose i have fetched 10 questions then it will display tabs as 1 2 3 4 5 6 7 8 9 10. and currently what ever question is displaying on webpage that tab will be highlighted.
my question is how can i make these tabs as link so that when i click on that tab it will show that question on the webpage.
View:
<div id="container">
<?php echo "<br />" ?>
<?php
$attributes=array('id'=>'testForm');
echo form_open('attemptTest/getquestion/',$attributes);
?>
<div id="body">
<?php
$totq=explode(",",$test['random_question_no']);
$totq=array_sum($totq);
$totalQuestions=array_sum(explode(",",$test['random_question_no']));
$selected_answers=explode(',',$result['selected_answers']);
$selected_answers=$selected_answers[$qno-1];
?>
<input type="hidden" name="qno" value="<?=$qno?>" id="qno">
<input type="hidden" name="direction" value="N" id="direction">
<input type="hidden" name="submitTest" value="0" id="submitTest">
<input type="hidden" name="time1" value="<?php $time_taken=explode(',',$result['time_taken']); echo (array_sum($time_taken)+$result['iniTime']);?>" >
<code><table><tr><td valign="top" >Q<?=$qno?>) </td><td style="word-break: break-word;"><?=$question['question']?></td></tr></table></code>
<?php $option=explode(',',$question['options']);
foreach($option as $option){
$ans="ans".$qno;
?>
<div id="qoption">
<table><tr><td style="width:18px;" valign=top ><input type="radio" value="<?=$option?>" name="answer" <?php if($selected_answers==$option){ echo "checked";} ?> > </td><td><?=$option?></td></tr></table>
</div>
<?php
}
?>
<br/>
<table><tr><td><?php if($qno>="2"){ ?><div class="big_button" style="width:70px;"><center><a href="javascript:document.getElementById('direction').value='B';document.getElementById('testForm').submit();" id="big_button" >Back</a></center></div><?php } ?></td><td> </td><td>
<?php if($qno<$totalQuestions){ ?><div class="big_button" style="width:70px;"><center>Next</center></div> <?php } ?>
</td><td> </td>
<td>
<div class="big_button" style="width:120px;"><center>
Submit Test
</center></div>
</td></tr></table>
<br/>
</div></div>
<table id="all_questions">
<tr>
<?php $i=0; foreach($questions_all as $key=>$value){$i++;?>
<td class="<?php if($qno==$key+1){echo "active ";}?> <?php if(isset($_COOKIE['answered_'.$value])){if($_COOKIE['answered_'.$value]!=0){ echo "green";}} ?>" ><?=$key+1?></td><?php if($i==4){$i=0; echo "</tr><tr>";}?>
<?php }?>
</tr>
</table>
<style>
#all_questions td{
width:40px;
height:40px;
border-radius:3px;
margin:5px;
text-align:center;
vertical-align:middle;
}
.active {
background-color:red;
}
.green {
background-color:green;
}
</style>
Controller:
<?php
class attemptTest extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('test_model');
$this->load->model('attempt_model');
$this->load->helper('html');
$this->load->helper('url');
$this->load->library('session');
// if not logged in redirect
if(($this->session->userdata('logged_in'))!='TRUE'){ redirect('login'); }
}
public function index($tid='',$access_token='0')
{
$this->load->helper('cookie');
$this->load->library('form_validation');
// if test cookies exist bypass to getquestion function
if(isset($_COOKIE['tid']) && isset($_COOKIE['qids']) && isset($_COOKIE['resultid']) && isset($_COOKIE['qno']) && isset($_COOKIE['access_token']))
{
$this->getquestion($_COOKIE['qno'],$_COOKIE['qids'],$_COOKIE['tid']);
}
else
{
// else started new test so check access token cookie exist for test if not redirect
if(!isset($_COOKIE['access_token'])){
redirect('test/view/'.$tid);
}
// getting test data
$data['test'] = $this->test_model->get_test($tid,'0');
// getting multi dimensonal arrays of question ids which are going to assign user
$data['qids'] = $this->attempt_model->get_questions_id($data['test']);
// make one dimensonal array
$qid=array();
foreach($data['qids'] as $question)
{
$qid[]=$question['qid'];
}
// convert array into string
$qids=implode(",",$qid);
// insert result row
$data['resultid']=$this->attempt_model->insert_result_row($data['test'],$qids);
// creating required cookies
$cookie = array('name'=>'resultid','value'=>$data['resultid'],'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qids','value'=>$qids,'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'tid','value'=>$data['test']['tid'],'expire'=>'86500');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qno','value'=>'0','expire'=>'86500');
$this->input->set_cookie($cookie);
$this->getquestion('0',$qids,$tid,$data['resultid']);
}
}
public function getquestion($qno=0,$qids=0,$tid=0,$resultid=0)
{
if(!isset($_COOKIE['access_token'])){
redirect('index');
}
$this->load->helper('cookie');
$this->load->library('form_validation');
//define question ids
if(isset($_COOKIE['qids'])){
$qids=$_COOKIE['qids'];
}else{
$qids=$qids;
}
$eqid=explode(",",$qids);
//addition od code by vivek
$data['questions_all'] = $eqid;
//define test id
if(isset($_COOKIE['tid'])){
$tid=$_COOKIE['tid'];
}else{
$tid=$tid;
}
if(isset($_COOKIE['resultid'])){
$resultid=$_COOKIE['resultid'];
}
$iniTime = $this->attempt_model->get_result($resultid);
$test_time = $this->test_model->get_test($tid,'0');
// check time
if(((($test_time['test_time']*60)-(time()-$iniTime['iniTime']))<="0") || ((time())>($test_time['end_time']))){
$this->submit('Time Over');
return;
}
// define question no.
if(isset($_POST['qno'])){
$qno=$_POST['qno'];
$pqid=$eqid[$qno-1];
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
$time1=$_POST['time1'];
}else{ $answer='NULL'; $time1=$_POST['time1']; }
$this->attempt_model->submit_answer($_COOKIE['resultid'],$qno,$_COOKIE['tid'],$pqid,$answer,$time1);
}
if(isset($_POST['submitTest']))
{
if($_POST['submitTest']=='1')
{
$this->submit();
return;
}
}
// next question
if(isset($_POST['direction']))
{
// if direction is for next question add one
if($_POST['direction']=='N'){
$data['qno']=$qno+1; $qid=$eqid[$qno];
}
else
{
// else subtract one
$data['qno']=$qno-1;
$qid=$eqid[$qno-2];
}
}
else{
$data['qno']=$qno+1;
$qid=$eqid[$qno];
}
// getting earlier submitted answer if any
$data['result'] = $this->attempt_model->get_result($resultid);
// getting test data
$data['test'] = $this->test_model->get_test($tid,'0');
// getting question data
$data['question'] = $this->attempt_model->get_question($qid);
// load header page
$this->load->view('header_attemptTest.php',$data);
// load attempt test body page
$this->load->view('attemptTest',$data);
// load footer
$this->load->view('footer');
// update cookies with question number
$cookie = array('name'=>'qno','value'=>$data['qno'],'expire'=>'86500');
$this->input->set_cookie($cookie);
}
public function submit($msg='')
{
$this->load->helper('cookie');
$this->attempt_model->submit_test($_COOKIE['resultid'],$_COOKIE['tid']);
// page title meassage
$data['title']="Test Submitted";
$data['msg']=$msg;
$data['resultid']=$_COOKIE['resultid'];
$this->load->view('header.php',$data);
$this->load->view('submitTest',$data);
$this->load->view('footer');
// unset test cookies
$cookie = array('name'=>'resultid','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qids','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'tid','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'qno','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
$cookie = array('name'=>'access_token','value'=>'','expire'=>'1');
$this->input->set_cookie($cookie);
}
}
?>
Model:
<?php
class attempt_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_questions_id($test)
{
$noqs=explode(',',$test['random_question_no']);
$sids=explode(',',$test['subject_ids']);
foreach($noqs as $key => $noq){
$sid=$sids[$key];
if($key==0){
$sql="(select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
}else{
$sql=$sql."UNION (select qid from questionBank where sid='$sid' order by rand() LIMIT $noq )";
}
}
$query = $this->db->query($sql);
$questions=$query->result_array();
return $questions;
}
// return single question
public function get_question($qid)
{
$sql="select * from questionBank where qid='$qid' ";
$query = $this->db->query($sql);
$questions=$query->row_array();
return $questions;
}
// return multiple questions
public function get_questions($qids)
{
$sql="select * from questionBank where qid in($qids) ORDER BY FIELD(qid, $qids) ";
$query = $this->db->query($sql);
$questions=$query->result_array();
return $questions;
}
public function get_result($resultid)
{
$sql="select * from result where result_id='$resultid' ";
$query = $this->db->query($sql);
$result=$query->row_array();
return $result;
}
public function submit_answer($resultid,$qno,$tid,$qid,$posted_answer,$time1)
{
$sql="select * from result where result_id='$resultid' ";
$query1 = $this->db->query($sql);
$result=$query1->row_array();
// getting correct answer from question
$sql="select answer from questionBank where qid='$qid' ";
$query = $this->db->query($sql);
$answer=$query->row_array();
//compare posted answer with correct answer
$correct_answer=explode(",",$result['correct_answer']);
$selected_answers=explode(",",$result['selected_answers']);
$time_taken=explode(",",$result['time_taken']);
$time2=(time()-$time1);
$time_taken[$qno-1]=($time_taken[$qno-1])+$time2;
if($posted_answer!='NULL'){
if($posted_answer==$answer['answer']){ $correct_answer[$qno-1]="1"; }else{ $correct_answer[$qno-1]="0"; }
$selected_answers[$qno-1]=$posted_answer;
}
$correct_answer=implode(",",$correct_answer);
$selected_answers=implode(",",$selected_answers);
$time_taken=implode(",",$time_taken);
// Update result row
$data = array('correct_answer' => $correct_answer,'selected_answers' => $selected_answers,'time_taken' => $time_taken);
$this->db->where('result_id', $resultid);
$this->db->update('result', $data);
}
public function submit_test($resultid,$tid)
{
// getting test data
$sql="select * from test where tid='$tid' ";
$query1 = $this->db->query($sql);
$test=$query1->row_array();
// getting result data
$sql="select * from result where result_id='$resultid' ";
$query2 = $this->db->query($sql);
$result=$query2->row_array();
// calculating result
$correct_answer=$result['correct_answer'];
$correct_answer=str_replace("2","0",$correct_answer);
$correct_answer=explode(",",$correct_answer);
$correctans=array_sum($correct_answer);
$wrong_answer=$result['correct_answer']; $wrong_answer=explode(",",$wrong_answer); $wrong_ans=count( array_keys( $wrong_answer, "0" ));
$total_marks=$correctans-$wrong_ans*$test['minus_mark'];
$percentage=($total_marks/$result['total_question'])*100;
if($percentage>=$test['reqpercentage'])
{
$status="1";
}
else
{
$status="0";
}
// update result row
$data = array('obtained_percentage' => $percentage,'status' => $status,'submitTime'=>time());
$this->db->where('result_id', $resultid);
$this->db->update('result', $data);
}
public function insert_result_row($test,$qids)
{
$totq=explode(",",$test['random_question_no']);
$totqs=array_sum($totq);
$temp_value=array();
for($i=0; $i < $totqs; $i++)
{
$temp_value[$i]='2';
}
$temp_value=implode(",",$temp_value);
$temp_time=array();
for($j=0; $j < $totqs; $j++)
{
$temp_time[$j]='0';
}
$temp_time=implode(",",$temp_time);
$uid=$this->session->userdata('uid');
// insert row
$data=array('uid'=>$uid,'tid'=>$test['tid'],'total_question'=>$totqs,'correct_answer' => $temp_value,'time_taken' => $temp_time, 'selected_answers' => $temp_value,'question_ids'=>$qids,'status'=>'2','iniTime'=>time());
$this->db->insert('result', $data);
return $this->db->insert_id();
}
}
?>
I heaven't read your code but I suppose you would want this ;)
In this case, we assume this code is all in index.php file.
This is how you link to your question (or you can redirect there in any other way you want):
<?php
$id = -1;
if (isset($_GET["questionid"])) {
$id = $_GET["questionid"];
}
?>
<a href="index.php?questionid=1" <?php if($id == 1){echo " class='active'"} ?>>Question 1</a>
<a href="index.php?questionid=2" <?php if($id == 2){echo " class='active'"} ?>>Question 2</a>
<a href="index.php?questionid=3" <?php if($id == 3){echo " class='active'"} ?>>Question 3</a>
And here is the code how to show question with id questionid.
<?php
if ($id > -1) {
showQuestion($id);
// = your code to show question with id questionid
}
else {
echo "No question selected";
}
?>
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
I want to Sum of Common Division Between two number,
in below is my code , and output is maghsom moshtarak is=24612
but Goal is 12 only;
please help me,
<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo"maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($j%$i==0)&&($h%$i==0))
{
Sum($i);
}
}
}
function Sum($i)
{
$i+=$i;
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>
<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
Sum($i);
}
}
}
function Sum($i)
{
/* no need to use $i+=$i */
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>
** Refined Code **
<?php
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
echo $i;
}
}
}
SumCommon($j,$h);
?>
I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';