New to programming and looking for help please,
i need to the set the value of $b_num based on the value of $egg_type i have tried using an if statement but not having any luck
`
$egg_type = $row["egg_type"] ;
if ($egg_type == 'M/S Select Farm')
{
$b_num = '1';
}
if ($egg_type = 'Free Range')
{
$b_num = '1';
}
if ($egg_type = 'Barn')
{
$b_num = '2';
}
if ($egg_type =='Intensive')
{
$b_num = '3';
}
if ($egg_type == 'Organic')
{
$b_num = '0';
}
if ($egg_type == 'Brioche')
{
$b_num = '3';
}
`
Tried the if statement but the value didnt change,
First you've to check if your $egg_type variable is being set in order not to make errors later
Second you can do it like this
if(isset($egg_type)) {
if(in_array($egg_type, ['M/S Select Farm', 'Free Range'])) {
$b_num = 1;
} elseif(in_array($egg_type, ['Intensive', 'Brioche'])) {
$b_num = 3;
} elseif($egg_type === 'Organic') {
$b_num = 0;
} elseif($egg_type === 'Barn') {
$b_num = 2;
}
}
Maybe $egg_type is null value. Therefore,you can improve your code (PHP 8.0 version) and set default value for b_num like this:
$egg_type = $row["egg_type"] ;
$b_num = match($egg_type) {
'M/S Select Farm' => 1,
'Free Range' => 1,
'Barn' => 1,
'Intensive' => 1,
'Organic' => 1,
'Brioche' => 1,
default => 'nothing any value for b_num'
};
echo $b_num;
In am getting Undefined variable: score (View: C:\Users\Sarthak\blog\resources\views\submitquestion.blade.php) in blade view when executing :
Controller part
public function question(Request $request)
{
static $startscore = 0;
$getidvalue = Input::get('getid');
$getanswervalue = Input::get('getanswer');
$dbscore = DB::table('5question')->select('question_id', 'correct_answer', 'question_marks')->where('question_id', '=', $getidvalue)->get();
foreach($dbscore as $value) {
if ($getanswervalue == ($value->correct_answer)) {
$getscore = $startscore + $value->question_marks;
}
elseif ($getanswervalue == null) {
$emptyvalue = - 1;
$getscore = $startscore + $emptyvalue;
}
else {
$novalue = 0;
$getscore = $startscore + $novalue;
}
}
echo "$getscore";
Session::push('getscoresession', $getscore);
$getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
Session::push('answer', $getsession);
// return response()->json(['qid'=>$getidvalue,'answer'=>$getanswervalue]);
$score = array_sum(Session::get("getscoresession"));
// return view('submitquestion',compact('score'));
return view('submitquestion', ['score' => $score]);
}
Blade part :
You have submitted quiz and your score is : {{ $score }}>
LOGOUT
try this one:
return view('submitquestion', compact('score'));
you can pass variable form controller to view using with and compact:
$request->session()->put('getscoresession', $getscore);
$getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
$request->session()->put('answer', $getsession);
if ($request->session()->has('getscoresession')) {
$score = array_sum($request->session()->get("getscoresession"));
}else{
$score = 0;
}
using with :
$score= 10;
return view('submitquestion')->with('score',$score);
using compact :
return view('submitquestion',compact('score'));
Try it. If you get error from your controller send $score = 10;
return view('submitquestion')->with(['score' => $score]);
I tried to find solutions
but i always get this error
You must specify an index to match on for batch updates.
public function editcompanionship()
{
$companionship_id = $this->input->post('companionship_id[]');
$missionary_one_id = $this->input->post('missionary_one_id[]');
$missionary_two_id = $this->input->post('missionary_two_id[]');
$missionary_three_id = $this->input->post('missionary_three_id[]');
$value_batch_update = array();
for($i=0; $i<count($companionship_id); $i++):
$value_batch_update[$i] = array(
'missionary_one_id' => $missionary_one_id[$i],
'missionary_two_id' => $missionary_two_id[$i],
'missionary_three_id' => $missionary_three_id[$i],
'modified' => date('Y-m-d H:i:s', time()),
);
endfor;
$this->db->where('companionship_id',$companionship_id[$i]);
$this->db->update_batch('pcdom_companionship',$value_batch_update);
$this->session->set_flashdata("success",alert("alert-success","Updated Successfully!"));
redirect(base_url('mrec/companionship'));
exit();
}
Can Any body knows?
The 3rd param of method update_batch() is required.
public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
{
....
if ($index === NULL)
{
return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
}
...
I have comeup with strange problem in cakephp 3.4. I am running filter query on i18n content like this.
if($this->request->query("q")){
$this->paginate["conditions"][$this->ContractTypes->translationField('title').' LIKE'] = '%'.$this->request->query("q").'%';
}
but following call is ending up in Database error
$records = $this->paginate($this->ContractTypes);
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ContractTypes_title_translation.content' in 'where clause' SELECT (COUNT(*)) AS `count` FROM contract_types ContractTypes WHERE ContractTypes_title_translation.content like :c0
The paginator's count query is not joing i18n table. What is the best approach to solve this problem.
Thanks in advance,
I have solved this by creating my custom paginator component by editing the paginate function. My paginator contains following code incase somebody else is facing the same problem.
namespace Console\Controller\Component;
use Cake\Controller\Component\PaginatorComponent as BasePaginator;
class PaginatorComponent extends BasePaginator
{
public function paginate($object, array $settings = [])
{
$query = null;
if ($object instanceof QueryInterface) {
$query = $object;
$object = $query->repository();
}
$alias = $object->alias();
$options = $this->mergeOptions($alias, $settings);
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);
$options += ['page' => 1, 'scope' => null];
$options['page'] = (int)$options['page'] < 1 ? 1 : (int)$options['page'];
list($finder, $options) = $this->_extractFinder($options);
if (empty($query)) {
$query = $object->find($finder, $options);
} else {
$query->applyOptions($options);
}
$cleanQuery = clone $query;
// My Modification Starts Here
$table = $cleanQuery->repository();
$results = $query->all();
$numResults = count($results);
$count = $numResults ? $cleanQuery->select([
"count"=>$cleanQuery
->func()
->count($table->alias().'.'.$table->primaryKey())
])->first()->count : 0;
// My Modification ends Here
$defaults = $this->getDefaults($alias, $settings);
unset($defaults[0]);
$page = $options['page'];
$limit = $options['limit'];
$pageCount = (int)ceil($count / $limit);
$requestedPage = $page;
$page = max(min($page, $pageCount), 1);
$request = $this->_registry->getController()->request;
$order = (array)$options['order'];
$sortDefault = $directionDefault = false;
if (!empty($defaults['order']) && count($defaults['order']) == 1) {
$sortDefault = key($defaults['order']);
$directionDefault = current($defaults['order']);
}
$paging = [
'finder' => $finder,
'page' => $page,
'current' => $numResults,
'count' => $count,
'perPage' => $limit,
'prevPage' => $page > 1,
'nextPage' => $count > ($page * $limit),
'pageCount' => $pageCount,
'sort' => key($order),
'direction' => current($order),
'limit' => $defaults['limit'] != $limit ? $limit : null,
'sortDefault' => $sortDefault,
'directionDefault' => $directionDefault,
'scope' => $options['scope'],
];
if (!$request->getParam('paging')) {
$request->params['paging'] = [];
}
$request->params['paging'] = [$alias => $paging] + (array)$request->getParam('paging');
if ($requestedPage > $page) {
throw new NotFoundException();
}
return $results;
}
}
I am trying to use json encode for the first time and need some insight to what I am doing wrong.
It should look like this:
{ teams : [ ["2147483647", "9"],["2147483647", "6"],["2147483647", "4"],["11", "2147483647"],["5", "2147483647"],["12", "8"],["10", "3"],["2147483647", "7"], ], results : [ [ [[0, 1],[0, 1],[0, 1],[1, 0],[1, 0],[0, 0],[0, 0],[0, 1],], [[0, 0],[0, 0],[0, 0],[0, 0],], [[0, 0],[0, 0],], [[0, 0],[0, 0]] ] ] }
But the data being returned looks like this:-
{"teams":[["2147483647","10","5","12","11","2147483647","2147483647","2147483647"],["7","3","2147483647","8","2147483647","4","6","9"]],"results":[["0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0"]]}
Code:
public function getAutoCompleteData($tournID, $db)
{
$max = $this->max($tournID);
$one = $this->tourn_info("1on1", $tournID);
$total_matches = $max;
$after_matches = $max / 2;
$matches = $db->query($this->select("*", "matches", "leagueID='{$tournID}' AND league='tourn'"));
while ($row = $db->fetchAssoc($matches)) {
$clan1 = $this->getname($row['clan1'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 0);
$clan2 = $this->getname($row['clan2'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 1);
if ($row['matchno'] <= $after_matches) {
$clan_one[] = $row['clan1'];
$clan_two[] = $row['clan2'];
$score_one[] = $row['score1'];
$score_two[] = $row['score2'];
}
}
$data = array(
'teams' => array(
$clan_one,
$clan_two
),
'results' => array(
$score_one,
$score_two
)
);
return $data;
}
Where it shows teams, it should close the bracket ] every two teams?
Hope someone can help.
Not saying this will fix your problem, but it may help you to understand PHP.
class mustBeAClass{
protected function max($tnid){
// must have this function - can be public for outside method use
}
protected function tourn_info($typ, $tnid){
// must have this function - can be public for outside method use
}
protected function getname($arg0, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6 = 0){
/* must have this function - can be public for outside method use
notice that $arg6 is how you assign default to arguments */
}
public function getAutoCompleteData($tournID, $db){
$db->open();
$max = $this->max($tournID); $one = $this->tourn_info("1on1", $tournID);
// what is the point of this ---->>>> $total_matches = $max;
$after_matches = $max / 2;
// notice query issue below
$matches = $db->query("SELECT * FROM matches WHERE leagueID ='$tournID' && league='tourn'"));
// while loop has problems
if($matches->num_rows < 1){
die('You have no matches');
}
else{
while($row = $db->fetch_assoc()){
$clan1 = $this->getname($row['clan1'], $tournID, 'tourn', null, 1, 1, 0);
$clan2 = $this->getname($row['clan2'], $tournID, 'tourn', null, 1, 1, 1);
if($row['matchno'] <= $after_matches) {
$clan_one[] = $row['clan1'];
$clan_two[] = $row['clan2'];
$score_one[] = $row['score1'];
$score_two[] = $row['score2'];
}
}
$data = array(
'teams' => array($clan_one, $clan_two),
'results' => array($score_one, $score_two)
);
}
$matches->free(); $db->close();
return $data;
}
}
$cls = new mustBeAClass;
echo json_encode($cls->getAutoCompleteData($tournId, $db));
Of course use the correct values for $tournId and your new mysqli(/*args*/) for $db.