I did search for this but may be my search sucks. So posting it here..my code below
while( $row = mysqli_fetch_array($queryRecords) ) {
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
$marksheet = calculatepercent(totalmark),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
Here I call function calculatepercent(totalmark) so that the function returns a value and store that in $marksheet. But my problem is this does not work i.e I cannot store the result of calculatepercent(totalmark) because I cannot access 'totalmark'
Forget performance for a minute but how do you make this work? (If you have tips for performance that's a bonus too! ) - Thanks Coders!
RR
Try like this.Send $row['totalmark'] to the calculatepercent() function.
while( $row = mysqli_fetch_array($queryRecords) ) {
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
$marksheet => calculatepercent($row['totalmark']),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
I guess I was more desperate than any one else to get an answer! So after trying many things I got it working and sharing it here for any one else
while( $row = mysqli_fetch_array($queryRecords) ) {
$marksheet = calculatepercent($row['totalmark'];
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
//move the fn 'calculatepercent' outside while loop $marksheet => //calculatepercent($row['totalmark']),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
Related
I have an array of arrays, as such
$statuses = array(
[0] => array('id'=>10, 'status' => 'active'),
[1] => array('id'=>11, 'status' => 'closed'),
[2] => array('id'=>12, 'status' => 'active'),
[3] => array('id'=>13, 'status' => 'stopped'),
)
I want to be able to make a new array of arrays and each of those sub arrays would contain the elements based on if they had the same status.
The trick here is, I do not want to do a case check based on hard coded status names as they can be random. I want to basically do a dynamic comparison, and say "if you are unique, then create a new array and stick yourself in there, if an array already exists with the same status than stick me in there instead". A sample result could look something like this.
Ive really had a challenge with this because the only way I can think to do it is check every single element against every other single element, and if unique than create a new array. This gets out of control fast if the original array is larger than 100. There must be some built in functions that can make this efficient.
<?php
$sortedArray = array(
['active'] => array(
array(
'id' => 10,
'status' => 'active'
),
array(
'id' => 12,
'status' => 'active'
)
),
['closed'] => array(
array(
'id' => 11,
'status' => 'active'
)
),
['stopped'] => array(
array(
'id' => 13,
'status' => 'active'
)
),
)
$SortedArray = array();
$SortedArray['active'] = array();
$SortedArray['closed'] = array();
$SortedArray['stopped'] = array();
foreach($statuses as $Curr) {
if ($Curr['status'] == 'active') { $SortedArray['active'][] = $Curr; }
if ($Curr['status'] == 'closed') { $SortedArray['closed'][] = $Curr; }
if ($Curr['status'] == 'stopped') { $SortedArray['stopped'][] = $Curr; }
}
You can also do it with functional way though it's pretty the same like Marc said.
$sorted = array_reduce($statuses, function($carry, $status) {
$carry[$status['status']][] = $status;
return $carry;
}, []);
my php code generate a non-Valid json output error
my php code :
$questions = array();
while($question = mysql_fetch_array($result, MYSQL_ASSOC)) {
$questions[] = array('question'=> $question);
}
print_r ($questions);
$newQuestions = array('questions' => array());
foreach($questions as $key => $question){
$newQuestion = array(
'question' => $question['question']['question'],
'correct' => $question['question']['correct'],
'answers' => array(
$question['question']['answer1'],
$question['question']['answer2'],
$question['question']['answer3'],
$question['question']['answer4']
)
);
$newQuestions['questions'][] = $newQuestion;
}
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
table fields :
Question :
correct :
answer 1 :
answer 2 :
answer 3 :
answer 4 :
Example :
Question : is php a good language ?
correct : 1
answer 1 : yes
answer 2 : no
answer 3 : maybe
answer 4 : good
the output is OK, and formated as I want.
output sample : http://pastebin.com/eefS7KYW
I am sure my php code is correct but I don't know where is exactly the issue !!
==============
Fixed : it was just two echo $output !
Seems like you are doing a lot of variable passing which gets very confusing, very quickly. Especially when all the variables are iterations of 'question'. It appears that you are creating an array from information pulled from a database in the format [questions[question,correct,answers[1,2,3,4]]] This code format may work better?
$newQuestions = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$newQuestions['questions'][] = array(
'question' => $row['question'],
'correct' => $row['correct'],
'answers' => array(
$row['answer1'],
$row['answer2'],
$row['answer3'],
$row['answer4']
)
);
}
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
Had a semicolon in the wrong place.Fixed the code above and test the code with the following:
<?php
$array = array(
array('question'=>'Question1',
'correct'=>3,
'answer1' => 'Q1Answer1',
'answer2' => 'Q1Answer2',
'answer3' => 'Q1Answer3',
'answer4' => 'Q1Answer4'
),
array('question'=>'Question2',
'correct'=>3,
'answer1' => 'Q2Answer1',
'answer2' => 'Q2Answer2',
'answer3' => 'Q2Answer3',
'answer4' => 'Q1Answer4'
),
array('question'=>'Question3',
'correct'=>3,
'answer1' => 'Q3Answer1',
'answer2' => 'Q3Answer2',
'answer3' => 'Q3Answer3',
'answer4' => 'Q1Answer4'
)
);
$newQuestions = array();
//while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($array as $row){
$newQuestions['questions'][] = array(
'question' => $row['question'],
'correct' => $row['correct'],
'answers' => array(
$row['answer1'],
$row['answer2'],
$row['answer3'],
$row['answer4']
)
);
}
print_r($newQuestions);
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
?>
first of, just wanted to let you know that I am a newbie at CI. but I am having trouble with this piece of code where is breaking and I can't seem to be able to find the answer anywhere.
for some reason the code is breaking at the first if statement.. if possible could you help me out understand what is really happening there?
Thank you all for your help!
function main
{
$this->load->model(getData) psudo code for this area...
}
---model---
function getData....
{
Sql = this->db->query(sql code that returns all the information required.)
$result = $sql->result_array();
$types = array ( 'EVENT|TIME' => array( 'id' => 1, 'name' => 'Regular' ),
'PROPOSITION|REGULAR' => array( 'id' => 2, 'name' =>'Propositions'),
'EVENT|TIME' => array( 'id' => 3, 'name' => 'Now' ),
'PROPOSITION|FUTURES' => array( 'id' => 4, 'name' => 'Future' ));
$var = array();
foreach ($result as $event) {
$cat = $event['type'] . '|' . $event['sub_type'];
$typeId = $types[$cat]['id'];
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))
{
if(!is_array($var[$event['event_id']]))
{
$var[$event['event_id']] = array( 'event_name' =>
$event['event_name'],'event_abbreviation' =>
$event['event_abbreviation']);
}
$var[$event['event_id']]['var'][$event['type_id']] = array(
'type_name' => $event['abbreviation'],'type_abbreviation' => $event['name']
);
}
$event[$event['event_id']]['var'][$event['type_id']]['types'][$typeId] =
$types[$cat]['name'];
}
return $myResults;
}
In this line
if(!is_array($var[$event['event_id']]['var']$event['type_id']]))
You are missing a [ somewhere. I'm guessing before $event['type_id'].
So replace with:
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))
Here is a piece of my code:
if($all_pages)
foreach ($all_pages as $page)
{
$all_hokms = $mysqli->query("SELECT * FROM qm_hokm WHERE page_id = ".$page['page_id']."");
if($all_hokms)
foreach ($all_hokms as $hokm)
{
$one_page_ahkams[] = array(
'hokm_id_inPage' => $hokm['hokm_id_inPage'],
'type' => $page['type'],
);
}
else
$one_page_ahkams[] = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams), //Here Is the PROBLEM : (((
);
}
else
$all_pages_data[] = array();
echo json_encode($all_pages_data);
As you see, I want to send multidimensial array() in a json format (from server to client), but how can correct the insertion of an array in an other, I mean that I need to add $one_page_ahkams array in $all_pages_data one, any ideas
$one_page_ahkams = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams);//this comma (,) causes the problem i guess**
);
An other way to do it :
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array(['one_page_ahkams']=>$one_page_ahkams);
);
Just put that: 'ahkams' => array($one_page_ahkams), but check the content of $one_page_ahkams
I have a query that returns multiple rows. I can't seem to find a way to store the rows in the $params array. Is there a way to loop throw and store each row in the $params variable
$aResult = $db->exec_sql($sql);
$params = array(
// where $aResult[o]'' would be row 1 [1] row 2 etc. //
'store_id' => $aResult[]['iStoreID'],
'user_id' => $aResult[]['iUserID'],
'store_name' => $aResult[]['cStoreName'],
'store_url' => $aResult[]['cStoreURL'],
'rid' => $aResult[]['cRID'],
'affiliate_id' => $aResult[]['iAffiliateID'],
'team_id' => $aResult[]['iTeamID'],
'bizOP' => $aResult[]['cDefaultBizOpp'],
'showBizOPp' => $aResult[]['iShowBizOppDropdown'],
'boPosting' => $aResult[]['iEnableBOPosting'],
'brandinglevel' => $aResult[]['iBrandingLevel']
);
thank you for your help
As simple as that:
$params = array();
foreach($aResult as $row) {
$params[] = array(
'store_id' => $row['iStoreID'],
'user_id' => $row['iUserID'],
'store_name' => $row['cStoreName'],
'store_url' => $row['cStoreURL'],
'rid' => $row['cRID'],
'affiliate_id' => $row['iAffiliateID'],
'team_id' => $row['iTeamID'],
'bizOP' => $row['cDefaultBizOpp'],
'showBizOPp' => $row['iShowBizOppDropdown'],
'boPosting' => $row['iEnableBOPosting'],
'brandinglevel' => $row['iBrandingLevel']
);
}
Without knowing the exact structure of the result array i guess you need something like this:
<?php
$params = array();
$mapping = array(
'store_id' => 'iStoredID',
'user_id' => 'iUserID',
// and so on...
);
foreach ($aResult as $row) {
$tempRow = array();
foreach ($row as $key => $value) {
$paramKey = isset($mapping[$key]) ? $mapping[$key] : $key;
$tempRow[$paramKey] = $value;
}
$params[] = $tempRow;
}
I use it like this
$aResult = mysql_query($sql);
while($data = mysql_fetch_array($result)) {
'store_id' = $aResult['iStoreID'];
}
At least that is the idea