I have trouble with my static array, i need to update with dynamic array can someone help me ? because will be so hard if i have alot data and i must create 1 by 1.
$kriteria = [C1,C2,C3,C4,C5,C6];
$alternatif = [ALT1,ALT2,ALT,ALT4,ALT5,ALT6,ALT7];
$nEntropy = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy[$i] = (((-1)/log(7)) *(
($probabilitas[0][0]*log($probabilitas[0][0]))+
($probabilitas[1][0]*log($probabilitas[1][0]))+
($probabilitas[2][0]*log($probabilitas[2][0]))+
($probabilitas[3][0]*log($probabilitas[3][0]))+
($probabilitas[4][0]*log($probabilitas[4][0]))+
($probabilitas[5][0]*log($probabilitas[5][0]))+
($probabilitas[6][0]*log($probabilitas[6][0]))
));
}
}
$nEntropy1 = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy1[$i] = (((-1)/log(7)) *(
($probabilitas[0][1]*log($probabilitas[0][1]))+
($probabilitas[1][1]*log($probabilitas[1][1]))+
($probabilitas[2][1]*log($probabilitas[2][1]))+
($probabilitas[3][1]*log($probabilitas[3][1]))+
($probabilitas[4][1]*log($probabilitas[4][1]))+
($probabilitas[5][1]*log($probabilitas[5][1]))+
($probabilitas[6][1]*log($probabilitas[6][1]))
));
}
}
$nEntropy2 = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy2[$i] = (((-1)/log(7)) *(
($probabilitas[0][2]*log($probabilitas[0][2]))+
($probabilitas[1][2]*log($probabilitas[1][2]))+
($probabilitas[2][2]*log($probabilitas[2][2]))+
($probabilitas[3][2]*log($probabilitas[3][2]))+
($probabilitas[4][2]*log($probabilitas[4][2]))+
($probabilitas[5][2]*log($probabilitas[5][2]))+
($probabilitas[6][2]*log($probabilitas[6][2]))
));
}
}
$nEntropy3 = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy3[$i] = (((-1)/log(7)) *(
($probabilitas[0][3]*log($probabilitas[0][3]))+
($probabilitas[1][3]*log($probabilitas[1][3]))+
($probabilitas[2][3]*log($probabilitas[2][3]))+
($probabilitas[3][3]*log($probabilitas[3][3]))+
($probabilitas[4][3]*log($probabilitas[4][3]))+
($probabilitas[5][3]*log($probabilitas[5][3]))+
($probabilitas[6][3]*log($probabilitas[6][3]))
));
}
}
$nEntropyy4 = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy4[$i] = (((-1)/log(7)) *(
($probabilitas[0][4]*log($probabilitas[0][4]))+
($probabilitas[1][4]*log($probabilitas[1][4]))+
($probabilitas[2][4]*log($probabilitas[2][4]))+
($probabilitas[3][4]*log($probabilitas[3][4]))+
($probabilitas[4][4]*log($probabilitas[4][4]))+
($probabilitas[5][4]*log($probabilitas[5][4]))+
($probabilitas[6][4]*log($probabilitas[6][4]))
));
}
}
$nEntropy5 = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
$nEntropy5[$i] = (((-1)/log(7)) *(
($probabilitas[0][5]*log($probabilitas[0][5]))+
($probabilitas[1][5]*log($probabilitas[1][5]))+
($probabilitas[2][5]*log($probabilitas[2][5]))+
($probabilitas[3][5]*log($probabilitas[3][5]))+
($probabilitas[4][5]*log($probabilitas[4][5]))+
($probabilitas[5][5]*log($probabilitas[5][5]))+
($probabilitas[6][5]*log($probabilitas[6][5]))
));
}
}
showb($nEntropy);
showb($nEntropy1);
showb($nEntropy2);
showb($nEntropy3);
showb($nEntropy4);
showb($nEntropy5);
this image for my results
as you can see my code really static and so hard if i have alot data
and also i need my results likes this
EDIT
the answers from Vörös Amadea are correct, but there are still some that are lacking, my second question regarding the results is still in large numbers. is there a way to display it only once?
as u can see on my image before, i need to remove duplicate data so only 1 for each is who will displayed and store it into array.
I have modified a little code from Vörös Amadea because if I don't modify it, I get an error "Undefined variable: probability"
this is the code that I have modified.
for($x=0;$x<1;$x++){
$nth = $x;
$entz = array();
for ($i=0;$i<count($kriteria);$i++)
{
for ($j=0;$j<count($alternatif);$j++)
{
${"entz$nth"}[$i] = (((-1)/log(7)) *(
($probabilitas[0][$nth]*log($probabilitas[0][$nth]))+
($probabilitas[1][$nth]*log($probabilitas[1][$nth]))+
($probabilitas[2][$nth]*log($probabilitas[2][$nth]))+
($probabilitas[3][$nth]*log($probabilitas[3][$nth]))+
($probabilitas[4][$nth]*log($probabilitas[4][$nth]))+
($probabilitas[5][$nth]*log($probabilitas[5][$nth]))+
($probabilitas[6][$nth]*log($probabilitas[6][$nth]))
));
}
}
}
showb(${"entz$nth"});
You can generate variable names in for loops like this.
Just change the value of $how_many_i_want.
$how_many_i_want = 3;
for($x=0;$x<$how_many_i_want;$x++){
generate_entropy($x);
}
function generate_entropy($nth){
$kriteria = ['C1','C2','C3','C4','C5','C6'];
$alternatif = ['ALT1','ALT2','ALT','ALT4','ALT5','ALT6','ALT7'];
${"nEntropy$nth"} = array();
for ($i=0;$i<count($kriteria);$i++){
for ($j=0;$j<count($alternatif);$j++){
${"nEntropy$nth"}[$i] = (((-1)/log(7)) *(
($probabilitas[0][$nth]*log($probabilitas[0][$nth]))+
($probabilitas[1][$nth]*log($probabilitas[1][$nth]))+
($probabilitas[2][$nth]*log($probabilitas[2][$nth]))+
($probabilitas[3][$nth]*log($probabilitas[3][$nth]))+
($probabilitas[4][$nth]*log($probabilitas[4][$nth]))+
($probabilitas[5][$nth]*log($probabilitas[5][$nth]))+
($probabilitas[6][$nth]*log($probabilitas[6][$nth]))
));
}
}
showb(${"nEntropy$nth"});
}
Related
Is there a way to reduce this if statements? I got a bit in trouble, because the var ($c1g, $c2g aso.) also changes in every if statement.
$c1 = $_POST['category_1'];
if($c1=='perfect')
{
$c1g = $perfect[1];
}
else if($c1=='good')
{
$c1g = $good[1];
}
else {
$c1g = $bad[1];
}
$c2 = $_POST['category_2'];
if($c2=='perfect')
{
$c2g = $perfect[1];
}
else if($c2=='good')
{
$c2g = $good[1];
}
else {
$c2g = $bad[1];
}
...
It still kind of smells, but if you had an array indexed on the perfect, good, bad:
$values = ['perfect' => [1=>1, 2=>2, 3=>3],
'good' => [1=>1, 2=>2, 3=>3],
'bad' => [1=>1, 2=>2, 3=>3]];
Then you could use the posted value:
$c1 = $_POST['category_1'];
$c1g = $values[$c1][1] ?? $values['bad'][1];
$c2 = $_POST['category_2'];
$c2g = $values[$c2][1] ?? $values['bad'][1];
And if your posted values were grouped in another dimension like $_POST['cats']['category_2'] then you could just loop over $_POST['cats'].
Hi i am iterating over Episodes getting array of authors and inside this loop i want to gather information about each author. But there is problem, i just need the information about each author once.
This is my approatch, but wrong. and the code i am trying to make. Please help. I tried also in_array, and array_filter but without success.
$presentUsers = [];
$pUi = 0;
if ($isAuthor == true){
if ($project->getType() == 1) {
$episodes = $project->getComic()->getComicEpisodes();
foreach ($episodes as $comicEpisode) {
foreach ($comicEpisode->getProject()->getAccount() as $author) {
if ($author->getUser()->getId() == $this->getUser()->getId()) {
$comicEpisode->setIsMine(true);
$comicEpisode->setRevenue($author->getRevenue());
$comicEpisode->setIncome($author->getIncome());
}
if (empty($presentUsers)){
$presentUsers[$pUi]['Id'] = $author->getUser()->getId();
$presentUsers[$pUi]['Username'] = $author->getUser()->getUsername();
$presentUsers[$pUi]['FirstName'] = $author->getUser()->getFirstName();
$presentUsers[$pUi]['LastName'] = $author->getUser()->getLastName();
$presentUsers[$pUi]['VisibleName'] = $author->getUser()->getVisibleName();
$presentUsers[$pUi]['AvatarFileName'] = $author->getUser()->getAvatarFileName();
$presentUsers[$pUi]['Occupation'] = $author->getUser()->getOccupation();
$presentUsers[$pUi]['LastOnline'] = $author->getUser()->getLastOnline();
$pUi++;
}else{
if (!in_array($presentUsers, ['Id'=>$author->getUser()->getId()]))
{
$presentUsers[$pUi]['Id'] = $author->getUser()->getId();
$presentUsers[$pUi]['Username'] = $author->getUser()->getUsername();
$presentUsers[$pUi]['FirstName'] = $author->getUser()->getFirstName();
$presentUsers[$pUi]['LastName'] = $author->getUser()->getLastName();
$presentUsers[$pUi]['VisibleName'] = $author->getUser()->getVisibleName();
$presentUsers[$pUi]['AvatarFileName'] = $author->getUser()->getAvatarFileName();
$presentUsers[$pUi]['Occupation'] = $author->getUser()->getOccupation();
$presentUsers[$pUi]['LastOnline'] = $author->getUser()->getLastOnline();
$pUi++;
}
}
}
}
}
}else{
die('You are not the author of this project.');
}
Okay i done it like this
if (empty($presentUsers)){
$presentUsers[$pUi]['Id'] = $author->getUser()->getId();
$presentUsers[$pUi]['Username'] = $author->getUser()->getUsername();
$presentUsers[$pUi]['FirstName'] = $author->getUser()->getFirstName();
$presentUsers[$pUi]['LastName'] = $author->getUser()->getLastName();
$presentUsers[$pUi]['VisibleName'] = $author->getUser()->getVisibleName();
$presentUsers[$pUi]['AvatarFileName'] = $author->getUser()->getAvatarFileName();
$presentUsers[$pUi]['Occupation'] = $author->getUser()->getOccupation();
$presentUsers[$pUi]['LastOnline'] = $author->getUser()->getLastOnline();
$pUi++;
}else{
$found = 0;
foreach ($presentUsers as $presentUser){
if ($presentUser['Id'] == $author->getUser()->getId()){
$found = 1;
break;
}
}
if ($found != 1)
{
$presentUsers[$pUi]['Id'] = $author->getUser()->getId();
$presentUsers[$pUi]['Username'] = $author->getUser()->getUsername();
$presentUsers[$pUi]['FirstName'] = $author->getUser()->getFirstName();
$presentUsers[$pUi]['LastName'] = $author->getUser()->getLastName();
$presentUsers[$pUi]['VisibleName'] = $author->getUser()->getVisibleName();
$presentUsers[$pUi]['AvatarFileName'] = $author->getUser()->getAvatarFileName();
$presentUsers[$pUi]['Occupation'] = $author->getUser()->getOccupation();
$presentUsers[$pUi]['LastOnline'] = $author->getUser()->getLastOnline();
$pUi++;
}
}
I am currently running a wordpress backend and want to display some tweets based on hastags on my website. For the general API request and database storage, I use this function:
private function parseRequest($json) {
$tmp = $json;
$result = array();
if (isset($json['statuses'])) {
$tmp = $json['statuses'];
}
if (isset($tmp) && is_array($tmp)){
foreach ($tmp as $t) {
$this->image = null;
$this->media = null;
$tc = new \stdClass();
$tc->feed_id = $this->id();
$tc->id = $t['id_str'];
$tc->type = $this->getType();
$tc->nickname = '#'.$t['user']['screen_name'];
$tc->screenname = (string)$t['user']['name'];
$tc->userpic = str_replace('.jpg', '_200x200.jpg', str_replace('_normal', '', (string)$t['user']['profile_image_url']));
$tc->system_timestamp = strtotime($t['created_at']);
$tc->text = $this->getText($t);
$tc->userlink = 'https://twitter.com/'.$t['user']['screen_name'];
$tc->permalink = $tc->userlink . '/status/' . $tc->id;
$tc->media = $this->getMedia($t);
#$tc->additional = array('shares' => (string)$t['retweet_count'], 'likes' => (string)$t['favorite_count'], 'comments' => (string)$t['reply_count']);
if ($this->isSuitablePost($tc)) $result[$tc->id] = $tc;
}
}
return $result;
}
Now I am looking for a function that counts all the variable in the "additional array together e.g. shares + likes + comments and sorts all posts based on the resulting number.
I am using the standard wordpress sql database. I cannot find a solution or I am just blind.
Thanks in regards
You could use a simple usort function:
usort($tc, function($a, $b) {
$a_sum = array_sum($a->additional);
$b_sum = array_sum($b->additional);
if ($a_sum == $b_sum) {
return 0;
}
return ($a_sum < $b_sum) ? -1 : 1;
});
I have a query that is bringing back all rows from the table instead of the fields I have specified. Can you see any mistakes in this? I'm using codeigniter.
Thanks in advance!
unset($conditions);
$conditions['conditions'] = array("accountid"=>$this->sessionInfo['database_account_id'],
"DATE_FORMAT(salestart,'%Y-%m-%d')"=>$today,
"shop"=>"london"
);
$conditions['group_by'] = "item";
$conditions['fields'] = "accountid, item, count(uniqueid) as totalitems, sum(options) as totaloptions, colour";
$today_sales = $this->Database_Model->selectData("sales",$conditions);
My model is:
public function selectData($table,$condition=array()) {
if(isset($condition['fields'])){
$fields = $condition['fields'];
}
else{
$fields = "*";
}
$this->Database->select('*');
$this->Database->from($table);
if(isset($condition['conditions'])){
$this->Database->where($condition['conditions']);
}
if(isset($condition['group_by'])){
$this->Database->group_by($condition['group_by']);
}
if(isset($condition['order_by'])){
$this->Database->order_by($condition['order_by']);
}
if(isset($condition['where_in'])){
$where_in = $condition['where_in'];
foreach($where_in as $key =>$value){
$this->Database->where_in($key,$value);
}
}
if(isset($condition['joins'])){
$joins = $condition['joins'];
foreach($joins as $join){
$this->Database->join($join['table'], $join['joinWith'],$join['type']);
}
}
$query = $this->Database->get();
return $query->result_array();
}
Change this
$this->Database->select('*');
to this
$this->Database->select($fields);
How to implode and insert values into the database in CodeIgniter?
I am creating multiple choice quiz script using CodeIgniter framework. I want to store user results like this:
id userid q_id answer_id time_taken
1 1 1,2,3,4,5 2,3,4,5,3 4,5,7,6,7
in my controller:
public function insert_result()
{
$this->load->model('quiz_models');
$user_id=$this->input->post('user_id');
$qq_id=$this->input->post('questionid');
$answer_id=$this->input->post('AnswerID');
$time_taken=$this->input->post('timetaken');
$question_no=$this->input->post('question_no');
$bd = "$question_no";
switch ($bd) {
case"1":
$data=array('user_id'=>$user_id,
'q_id'=>$qq_id,
'answer_id'=>$answer_id,
'time_taken'=>$time_taken);
$this->quiz_models->insert_result($data);
break;
case"2":
quiz_test();
break;
case"3":
quiz_test();
break;
case"4":
quiz_test();
break;
case"5":
quiz_test();
$this->session->unset_userdata('lastids');
break;
default:
echo "something is wrong";
}
}
public function quiz_test()
{
$this->load->model('quiz_models');
$quiz=$this->quiz_models->quiz_test();
foreach($quiz as $row){
$qid=$row->q_id;
$ans=$row->answer_id;
$time=$row->time_taken;
$a = array("$qq_id","$qid");
$b = array("$answer_id","$ans");
$c = array("$time_taken","$time");
$comma = implode(",",$a);
$comma1 = implode(",",$b);
$comma2 = implode(",",$c);
$data=array('q_id'=>$comma,
'answer_id'=>$comma1,
'time_taken'=>$comma2);
$this->quiz_model->update_result($data);
}
}
}
and Model:
function insert_result($data)
{
$this->dbb->insert('results',$data);
$sectio=$this->db->insert_id();
$this->session->set_userdata('lastids',$sectio);
}
function quiz_test()
{
$ses_id = $this->session->userdata('lastids');
$sql = "SELECT q_id, answer_id, time_taken FROM results WHERE id='$ses_id'";
$query = $this->dbb->query($sql);
$result = $query->result();
return $result;
}
function update_result($data){
$ses_id = $this->session->userdata('lastids');
$this->db->where('id',$ses_id);
$this->db->update('results',$data);
}
when i run it nothing happened,not showing any error where do i mistake?
pls help me what am i doing wrong
First of all - i think you've a major problem in your DB structure
Normalize your Data
You should prevent to store your information in the table like that.
It should be possible to normalize your data properly. If you dont know
how to do that the following link could be interesting:
Normalization in MYSQL
However a possible solution would be to structure your data:
In order to do that - create a save Method in your Model to split between update and insert - this model could look like
class Quiz_Models
{
private $arrPostData;
public function save($arrPostData = false)
{
$this->arrPostData = (!$arrPostData) ? $this->input->post() : $arrPostData;
$id = $this->session->userdata("lastids");
if ($id)
{
$query = $this->db
->select("*")
->from("results")
->where("id",$id)
->get();
if ($query->num_rows() == 1)
{
$this->update($query->row(0));
}
else return false;
}
else
{
$this->insert();
}
if ($this->arrPostData['question_no'] == 10) $this->session->unset_userdata("lastids");
}
private function update($objData)
{
$objCollection = new Quiz_Collection();
$objCollection->userId = $objData->userid;
$objCollection->id = $objData->id;
$arrData = explode($objData->q_id);
foreach($arrData AS $key => $quizId)
{
$objQuiz = new stdClass();
$objQuiz->q_id = $quizId;
$objQuiz->answer_id = explode($objData->answer_id)[$key];
$objQuiz->time_taken = explode($objData->answer_id)[$key];
$objCollection->append($objQuiz);
}
$objQuizFromPost = new stdClass();
$objQuizFromPost->q_id = $this->arrPostData["questionid"];
$objQuizFromPost->answer_id = $this->arrPostData['AnswerID'];
$objQuizFromPost->time_taken = $this->arrPostData['timetaken'];
$objCollection->addQuizFromPost($objQuizFromPost);
$this->db
->where("id",$objCollection->id)
->update("results",$objCollection->getDbData());
}
private function insert()
{
$objCollection = new Quiz_Collection();
$objCollection->userId = $this->arrPostData['user_id'];
$objQuizFromPost = new stdClass();
$objQuizFromPost->q_id = $this->arrPostData["questionid"];
$objQuizFromPost->answer_id = $this->arrPostData['AnswerID'];
$objQuizFromPost->time_taken = $this->arrPostData['timetaken'];
$objCollection->addQuizFromPost($objQuizFromPost);
$this->db->insert("results",$objCollection->getDbData());
$this->session->set_userdata("lastids", $this->db->insert_id());
}
}
as an addition you need a Collection Object (put this below your model)
class Quiz_Collection extends Array_Object
{
public $userId = 0;
public $id = 0;
public function addQuizFromPost($objQuiz)
{
if (intval($objQuiz->q_id) > 0)
{
foreach($this AS $key => $obj)
{
if ($obj->q_id == $objQuiz->q_id)
{
$this->offsetSet($key, $objQuiz);
return true;
}
}
$this->append($objQuiz);
return true;
}
return false;
}
public function sortQuizData($objA, $objB)
{
if ($objA->q_id == $objB->q_id) return 0;
return ($objA->q_id < $objB->q_id) ? -1 : 1;
}
public function getDbData()
{
$this->uasort(array($this,"sortQuizData"));
$arrData = $this->getArrayCopy();
$arrDbData = [
"userid" => $this->userId,
"q_id" => implode(array_map(function($obj){ return $obj->q_id;},$arrData),","),
"answer_id" => implode(array_map(function($obj){ return $obj->answer_id;},$arrData),","),
"time_taken" => implode(array_map(function($obj){ return $obj->time_taken;},$arrData),","),
];
return $arrDbData;
}
}
pS: this is just an instruction how you can do that in a proper way. Pls study this code. If you still don't understand whats going on, feel free to ask.