need an associative array from two mysql colums - mysql

i have a mysql table
||==========||==========||==========||
|| id || name || age ||
||==========||==========||==========||
|| 1 || joe || 10 ||
|| 2 || harry || 20 ||
|| 3 || jane || 45 ||
|| 4 || john || 56 ||
|| 5 || larry || 89 ||
|| 6 || henry || 23 ||
|| 7 || steve || 25 ||
|| 8 || eric || 56 ||
|| 9 || dave || 98 ||
|| 10 || mat || 56 ||
||==========||==========||==========||
i need the sql query that would make an associative array from this table and give me values like this
id=>age
1=>10
4=>56
i also need to make all the id's as a variable where there value would be the age like
$1 = 10
$4 = 56
or if i could add a prefix to the variables
$id_1 = 10
$id_4 = 56
thanks in advance

In PHP, mysql_fetch_assoc() takes the result of a query, and returns one row as an associative array. Call it repeatedly to get all rows.

if your using php for fetching your data you may use the function mysql_fetch_array example:
while($row = mysql_fetch_assoc($resource))
{
echo $row['name'].'<br>';
}
you may know more about mysql functions for php on this site: http://www.php.net/manual/en/book.mysql.php

$ids = '1, 4';
$sql = "select age from __TABLE__ where id in ( {$ids} )";
$link = mysql_connect('__SERVER__', '__DBUSER__', '__DB_PWD__') or die('connect error');
$db = mysql_select_db('__DBNAME__', $link) or die('db error');
$rs = mysql_query($sql, $link);
$result = array();
while($row = mysql_fetch_assoc($rs))
{
$result[] = $row;
}
extract(my_result($result));
var_dump($id_1);
var_dump($id_4);
function my_result($result, $prefix = 'id_')
{
$data = array();
foreach($result as $k => $v)
{
$data[$prefix . $v['id']] = $v['order_no'];
}
return $data;
}

Related

Data call using apend in controller

Hy. I'm learning about Laravel
im using append to enter data in select option
here is my append code
if($('.type').val()=="MAG") {
$('.tools').empty();
$('.tools').append('<?php echo $MAGRES ?>');
$(".tools").prop('disabled', false);
$('.level').empty();
$('.level').append('<?php echo $mlevel ?>');
$(".level").prop('disabled', false);
}
if($('.type').val()=="WAG") {
$('.tools').empty();
$('.tools').append('<?php echo $WAGRES ?>');
$(".tools").prop('disabled', false);
$('.level').empty();
$('.level').append('<?php echo $flevel ?>');
$(".level").prop('disabled', false);
}
Here is my controller
$openM =\App\eventcontroller::where('eventcontroller.eventID','=',$id)->where('type','MAG')->first();
$openF =\App\eventcontroller::where('eventcontroller.eventID','=',$id)->where('type','WAG')->first();
$mlevel='<option value="">-- Choose Levels --</option>';
$flevel='<option value="">-- Choose Levels --</option>';
if($openM->OneO == 1 || $openM->OneA == 1 || $openM->OneB == 1 || $openM->OneC == 1 || $openM->OneD == 1){$mlevel=$mlevel.'<option value="Level 1">Level {{$event->onename}} </option>';}
if($openM->TwoO == 1 || $openM->TwoA == 1 || $openM->TwoB == 1 || $openM->TwoC == 1 || $openM->TwoD == 1){$mlevel=$mlevel.'<option value="Level 2">Level {{$event->twoname}}</option>';}
if($openM->ThreeO == 1 || $openM->ThreeA == 1 || $openM->ThreeB == 1 || $openM->ThreeC == 1 || $openM->ThreeD == 1){$mlevel=$mlevel.'<option value="Level 3">Level {{$event->threename}}</option>';}
if($openM->FourO == 1 || $openM->FourA == 1 || $openM->FourB == 1 || $openM->FourC == 1 || $openM->FourD == 1){$mlevel=$mlevel.'<option value="Level 4">Level {{$event->fourname}}</option>';}
if($openM->FiveO == 1 || $openM->FiveA == 1 || $openM->FiveB == 1 || $openM->FiveC == 1 || $openM->FiveD == 1){$mlevel=$mlevel.'<option value="Level 5">Level {{$event->fivename}}</option>';}
if($openM->FigO == 1 || $openM->Fig1 == 1 ||$openM->Fig2 == 1){$mlevel=$mlevel.'<option value="FIG">FIG</option>';}
if($openF->OneO == 1 || $openF->OneA == 1 || $openF->OneB == 1 || $openF->OneC == 1 || $openF->OneD == 1){$flevel=$flevel.'<option value="Level 1">Level {{$event->onename}}</option>';}
if($openF->TwoO == 1 || $openF->TwoA == 1 || $openF->TwoB == 1 || $openF->TwoC == 1 || $openF->TwoD == 1){$flevel=$flevel.'<option value="Level 2">Level {{$event->twoname}}</option>';}
if($openF->ThreeO == 1 || $openF->ThreeA == 1 || $openF->ThreeB == 1 || $openF->ThreeC == 1 || $openF->ThreeD == 1){$flevel=$flevel.'<option value="Level 3">Level {{$event->threename}}</option>';}
if($openF->FourO == 1 || $openF->FourA == 1 || $openF->FourB == 1 || $openF->FourC == 1 || $openF->FourD == 1){$flevel=$flevel.'<option value="Level 4">Level {{$event->fourname}}</option>';}
if($openF->FiveO == 1 || $openF->FiveA == 1 || $openF->FiveB == 1 || $openF->FiveC == 1 || $openF->FiveD == 1){$flevel=$flevel.'<option value="Level 5">Level {{$event->fivename}}</option>';}
if($openF->FigO == 1 || $openF->Fig1 == 1 ||$openF->Fig2 == 1){$flevel=$flevel.'<option value="FIG">FIG</option>';}
there is no problem to display the options
but its print text :
'{{$event->onename}}' not data from $event->onename
i want it shows $event->onename data from database.
can anyone help me? thank you
Because you are using Blade syntax in your controller file, that is why it is displaying {{$event->onename}} instead of its actual value.
You can do like
if($openM->OneO == 1 || $openM->OneA == 1 || $openM->OneB == 1 || $openM->OneC == 1 || $openM->OneD == 1){
$mlevel= $mlevel."<option value=\"Level 1\">Level {$event->onename} </option>";
}
Or
if($openM->OneO == 1 || $openM->OneA == 1 || $openM->OneB == 1 || $openM->OneC == 1 || $openM->OneD == 1){
$mlevel = $mlevel.'<option value="Level 1">Level '.$event->onename.'</option>';
}

Friends of a user + mutual friend test with another user

I have this table that I use (but not only) for storing friends in a database :
user_1 | user_2 | status
where 'status' can be -1,0 or 1. Here, we will consider only cases where status are '0' (pending for user_1) or '1' (approved by user_2). I have the following query to look for pending/approved friends for a given $user :
SELECT user_1,user_2,status
FROM Friends
WHERE (user_2 = '$user' OR user_1 = '$user') AND status >= 0;
The goal here is to modify the query to also tell if a given $user2 is a common (approved) friend of $user1 and each (approved) friend of $user1.
After some researches, I figured out that the left join would do the trick, by setting another field to either NULL (if no mutual) or $user2. I would want to do it efficiently. I tried several shots, but no success around it.
Thanks by advance for your help
EDIT : For example, let's say we have the following entries :
a | b | 1
c | a | 1
c | b | 1
a | d | 1
I want to list the friends of 'a' and for each friend f of 'a', verify if 'b' is a common friend of f and 'a'. Also, f =/= b for the mutual test. The result of such a query would be :
a | b | 1 | NULL
c | a | 1 | b
a | d | 1 | NULL
Let me know if you need more clarification
As in MySQL query would be so complicated and slow, that I wouldn't use it myself, here's a solution in PHP, with only one query:
<?php
// $db = mysqli_connect(...);
function findMutualFriends($of,$mutual_with){
global $db;
$user_friends = array();
$mutual_friends = array();
$results = array();
$res = mysqli_query($db,"SELECT user_1,user_2,status FROM Friends WHERE ((user_2 = '$of' OR user_1 = '$of') OR (user_2 = '$mutual_with' OR user_1 = '$mutual_with')) AND status >= 0;";
while($row = mysqli_fetch_assoc($res)){
if($row['user_1'] == $of || $row['user_2'] == $of){
$user_friends[] = (($row['user_1'] == $of) ? $row['user_2'] : $row['user_1']);
}
if($row['user_1'] == $mutual_with || $row['user_2'] == $mutual_with){
$mutual_friends[(($row['user_1'] == $mutual_with) ? $row['user_2'] : $row['user_1'])] = 1;
}
}
foreach($user_friends as $friend){
if($mutual_firends[$friend]){
$results[] = $friend;
}
}
return $results;
}
?>
Please notice that it haven't been tested. May contain some minor syntax error, but should return an array of mutual friends.
I modified a bit the Flash Thunder's function post. Just tested with some modifications and it works ! Thanks again.
function findMutualFriends($pdo, $of,$mutual_with){
$user_friends = array();
$mutual_friends = array();
$results = array();
$query = "SELECT user_1,user_2,status FROM Friends WHERE ((user_2 = '$of' OR user_1 = '$of') OR (user_2 = '$mutual_with' OR user_1 = '$mutual_with')) AND status = 1;";
$prep = $pdo->prepare($query);
$res = $prep->execute();
$rows = $prep->fetchAll();
foreach ($rows as $row) {
if($row['user_1'] == $of || $row['user_2'] == $of) {
$user_friends[] = ($row['user_1'] == $of ? $row['user_2'] :$row['user_1']);
}
if($row['user_1'] == $mutual_with || $row['user_2'] == $mutual_with) {
$mutual_friends[($row['user_1'] == $mutual_with ? $row['user_2'] :$row['user_1'])] = true;
}
}
foreach($user_friends as $friend) {
$results[$friend] = $mutual_friends[$friend] == true ? true : false;
}
return $results;
}

How can I calculated dates from another column and another table?

I have 2 tables:
table 1:
|| *handtool_id* || *maintenance_interval_value* || *unit_unit_id* || *handtool_last_date_of_maintenance* || *handtool_next_date_of_maintenance* ||
|| 1 || 1 || 5 || 2014-11-07 || ||
|| 2 || 1 || 6 || 2014-11-07 || ||
|| 3 || 4 || 4 || 2014-11-07 || ||
table 2:
|| *unit_id* || *unit_name* || *unit_value* || *unit_parent_id* ||
|| 1 || Minute || 1 || 1 ||
|| 2 || Hour || 60 || 1 ||
|| 3 || Day || 1440 || 1 ||
|| 4 || Week || 10080 || 1 ||
|| 5 || Month || 32767 || 1 ||
|| 6 || Year || 525949 || 1 ||
What is the right syntax for calculating the handtool_next_date_of_maintenance from maintenance_interval_value and from unit_unit_id? Thank you
I have to say, it's wrong, and very confusing to change your question like this. You should rollback this question to the one Andrew Jones answered Nov 3, accept and upvote his answer, and then ask a new question.
That said, this would appear to get you something like what you're after (although how you arrived at figures of 32767 and 525949 is beyond me !?!)
SELECT *
, h.handtool_last_date_of_maintenance
+ INTERVAL h.maintenance_interval_value
* u.unit_value MINUTE x
FROM handtools h
JOIN units u
ON u.unit_id = h.unit_unit_id;
Whenever you insert to B, you want to insert into A. This is a good use of a MySQL trigger. I'm assuming an auto increment for web_content_id.
DELIMITER //
CREATE TRIGGER new_language_id
AFTER INSERT ON B
FOR EACH ROW
BEGIN
INSERT INTO A (web_content_const, i18n_language_codes_i18n_language_codes_id)
VALUES ('SERVICES_HEADING', #i18n_language_codes_id),
('SERVICES_MAIN_TEXT', #i18n_language_codes_id),
('SERVICES_1_HEADING', #i18n_language_codes_id),
('SERVICES_1_TEXT', #i18n_language_codes_id);
END;//
DELIMITER ;

Propel criteria? or is it even possible to create such query?

I have 3 tables: product, product_parameter, product_parameter_item.
Product and product parameter have no foreign references, product_parameter_item has product_id and product_parameter_id.
For example i have 4 products:
1
2
3
4
Product_parametetrs:
1 - Model
2 - Color
Product_parameter_item:
id - product_id - product_paramter_id - value:
1 - 1 - 1 - Toyota
2 - 1 - 2 - white
3 - 2 - 1 - Toyota
4 - 2 - 2 - black
5 - 3 - 1 - Citroen
6 - 3 - 2 - white
7 - 4 - 1 - Citroen
8 - 4 - 1 - black
How can I get all products with Model = Toyota, Color = Black ?
$c->addJoin(ProductPeer::ID, ProductParameterItemPeer::PRODUCT_ID);
$c->addJoin(ProductParameterItemPeer::PRODUCT_PARAMETER_ID, ProductParameterPeer::ID);
foreach ($product_params as $i => $param){
$c1 = $c->getNewCriterion(ProductParameterPeer::ID, $param->getId());
$c2 = $c->getNewCriterion(ProductParameterItemPeer::VALUE, $value);
$c1->addAnd($c2);
$c->addAnd($c1);
}
Doesnt work
$c->addJoin(ProductPeer::ID, ProductParameterItemPeer::PRODUCT_ID);
foreach ($product_params as $i => $param){
$c1 = $c->getNewCriterion(ProductParameterItemPeer::ID, $param->getId());
$c2 = $c->getNewCriterion(ProductParameterItemPeer::VALUE, $value);
$c1->addAnd($c2);
$c->addAnd($c1);
}
Doesnt work either
$c->addJoin(ProductParameterItemPeer::PRODUCT_PARAMETER_ID, ProductParameterPeer::ID);
foreach ($product_params as $i => $param){
$c->add(ProductParameterPeer::ID, $param->getId());
$c->add(ProductParameterItemPeer::VALUE, $value);
}
I have made this function to get ids of products and add them to Criteria. Didnt find another way :(
$product_params = ProductParameterPeer::getParamsForFilter();
$ids = false;
if (count($product_params)){
$sql = '';
$clause = '';
$clauses = array();
$make_query = false;
foreach ($product_params as $i => $param){
if ($values[$param->getAlias()]){
$is_last = end($product_params) == $param;
$sql .= '(SELECT product_id FROM `product_parameter_item` WHERE `value` = "'.$values[$param->getAlias()].'" AND `product_parameter_id` = '.$param->getId().' ) t'.$i.($is_last?' ':', ');
$clauses[] = 't'.$i.'.product_id';
$make_query = 'SELECT t'.$i.'.product_id FROM ';
}
}
if (count($clauses) == 1) $clause = 'WHERE '.$clauses[0];
else if (count($clauses) == 2) $clause = 'WHERE '.$clauses[0].'='.$clauses[1];
else if (count($clauses) > 2){
$clause = 'WHERE ';
for ($i = 1; $i < count($clauses); $i++){
$clause .= '('.$clauses[0].'='.$clauses[$i].')'.($i == count($clauses)-1 ? '' : ' AND ');
}
}
if ($make_query){
$query = $make_query.$sql.$clause;
$con = Propel::getConnection();
$statement = $con->prepare($query);
$statement->execute();
$ids = array();
while($stmt = $statement->fetch(PDO::FETCH_ASSOC)) {
$ids[] = (int)$stmt['product_id'];
}
}
}
return $ids;

How to get a parseJSON in Android through php

I have a part of code from php like this. Any two table tbl_gejala for change a sentence and tbl_hormon for move a Layout. Tbl_gejala with $id_g and tbl_hormon $id_h. and this php called by gejala.php.
I use 2 choice for this application, yes or no. $jawab == 1 for yes and $jawab == o for no.
$id_g = $_GET['id_g'];
$jawab = $_GET['a'];
$link = mysql_connect('localhost', 'root', '') or die ('Tidak bisa menampilkan');
mysql_select_db('hormon', $link) or die('Tidak bisa select');
if($id_g == 1 && $jawab == 1){
$query = "SELECT * from tbl_gejala where id_g = 3";
}elseif($id_g == 3 && $jawab == 1){
$query = "SELECT * from tbl_gejala where id_g = 5";
}elseif($id_g == 5 && $jawab == 1){
//intent activity and parseJSON with table hormon
$query = "SELECT * from tbl_hormon where id_h = 16";
}elseif($id_g == 5 && $jawab == 0){
$query = "SELECT * from tbl_gejala where id_g = 10";
}elseif($id_g == 10 && $jawab == 1){
$query = "SELECT * from tbl_hormon where id_h = 1";
}elseif($id_g == 10 && $jawab == 0){
$query = "SELECT * from tbl_hormon where id_h = 7";
I want to parseJSON with gejala.php in konsultasi.class
I have try this coding, but any problem by logic error. This coding like this:
void parseJSON(String response) {
try {
JSONObject json = new JSONObject(response);
listArray = new ArrayList<ListModel>();
JSONArray jArray = json.getJSONArray("Hormon");
log("lenght: " + jArray.length());
for (int i = 0; i < jArray.length(); i++) {
JSONObject jData = jArray.getJSONObject(i);
if(idPert.equals("5")&&yes.equals("1") ||
idPert.equals("7")&&yes.equals("1") ||
idPert.equals("11")&&yes.equals("1") ||
idPert.equals("11")&&no.equals("0") ||
idPert.equals("10")&&yes.equals("1") ||
idPert.equals("16")&&yes.equals("1") ||
idPert.equals("19")&&yes.equals("1") ||
idPert.equals("20")&&yes.equals("1") ||
idPert.equals("20")&&no.equals("0") ||
idPert.equals("8")&&yes.equals("1") ||
idPert.equals("17")&&yes.equals("1") ||
idPert.equals("17")&&no.equals("0") ||
idPert.equals("6")&&yes.equals("1") ||
idPert.equals("13")&&yes.equals("1") ||
idPert.equals("13")&&no.equals("0") ||
idPert.equals("12")&&yes.equals("1") ||
idPert.equals("14")&&yes.equals("1") ||
idPert.equals("15")&&yes.equals("1") ||
idPert.equals("18")&&yes.equals("1") ||
idPert.equals("21")&&yes.equals("1") ||
idPert.equals("22")&&yes.equals("1") ||
idPert.equals("10")&&no.equals("0")) {
id_h = jData.getString("id_h");
nm_penyakit = jData.getString("nm_penyakit");
definisi = jData.getString("definisi");
gejala = jData.getString("gejala");
penyebab = jData.getString("penyebab");
solusi = jData.getString("solusi");
gambar = jData.getString("gambar");
munculPenyakit = true;
}else{
idPert = jData.getString("id_g");
pertanyaan = jData.getString("gejala2")+"?";
munculPenyakit = false;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Please help me, thanks a lot before.