I want three element in associative array, so far am successful in getting two in the array.
$sql = "SELECT * FROM `notification_table` ";
$resultsd1 = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($resultsd1);
$associativeArray = array();
while ($row = mysqli_fetch_assoc($resultsd1))
{
$associativeArray[$row['name']] = $row['price'] ;
}
foreach($associativeArray as $k => $id){
echo $k."=>".$id .' ';
}
And am getting the response like this
name1=>24.725 name2=>24.265
Now i want to add another column in array as well and the name is column is notification_check .
Am not able to get how to add three columns in a single array. Any help will be appreciated.
I want the output like name1=>24.725=>yes_notification name2=>25.43=>no_notification
And when i print_r($row) is show this output Array ( [sno] => 1 [name] => name1 [price] => 23 [notification_check] => yes_notification)
You could shorten this and use mysqli_fetch_all to create an array of all of the data and then manipulate the array using array_column to create the index...
$result = mysqli_fetch_all($resultsd1, MYSQLI_ASSOC);
$associativeArray = array_column($result, null, 'name');
Related
I want to merge two of my columns (yanlis_cevaplar, cevap_icerik) into an array and this code here gives me only one column in array when I print it (yanlis_cevaplar).
How do I fix it?
$cevaplar = "SELECT yanlis_cevaplar FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap1 = array(); //create empty array
while ($row = $cevap_sonuc->fetch_array()) { //loop to get all results
$cevap1[] = $row; //grab everything and store inside array
}
$cevaplar2 = "SELECT cevap_icerik FROM cevaplar";
$cevap_sonuc2 = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap2 = array(); //create empty array
while ($row = $cevap_sonuc2->fetch_array()) { //loop to get all results
$cevap2[] = $row; //grab everything and store inside array
}
$tumcevaplar = array_merge($cevap1, $cevap2);
print_r($tumcevaplar);
Instead of making multiple queries, you can just fetch all the columns you want in one single query:
$cevaplar = "SELECT yanlis_cevaplar, cevap_icerik FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
// Now you can fetch all the rows straight away without any loop.
// The MYSQLI_ASSOC will return each row as an associative array
$result = $cevap_sonuc->fetch_all(MYSQLI_ASSOC);
print_r($result);
This will result in something like this:
Array
(
[0] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
[1] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
... and so on ..
)
If this isn't what you want, then you need to show us an example.
I also recommend that you go through some basic SQL tutorials. How SELECT works is SQL 101. Here's one of many guides: https://www.tutorialspoint.com/mysql/mysql-select-query.htm
I have a database structure that looks like the following:
I am trying to store the prices as a variables so that if I reference cage_linear_feet it will display 225.
I have tried the following code:
$sql = "SELECT * from pricing WHERE region = '$region' ";
$result = mysqli_query($conn, $sql);
while($rows=mysqli_fetch_assoc($result)){
$test[] = array($rows['field']=>$rows['price']);
}
print_r ($test);
This displays the following:
Array ( [0] => Array ( [cage_linear_feet] => 225 ) [1] => Array ( [cage_doors] => 1800 ) )
How do I access the "225" and "1800" values ensuring that if more records are added to the db it will still pick up the correct records. E.g as the $test1 which is cage_doors may change if records are added.
My opinion is that there is no point in wrapping the key-value pairs in their own separate arrays. I would change this line:
$test[] = array($rows['field']=>$rows['price']);
to this:
$test[$rows['field']] = $rows['price'];
The data association will be maintained in the key=>value pairs of one associative array ($test). So echo $test['cage_linear_feet'] ---> 225
If you want to set field as the array-key
$sql = "SELECT * from pricing WHERE region = '$region' ";
$result = mysqli_query($conn, $sql);
while($rows=mysqli_fetch_assoc($result)){
$test[$rows['field']] = $rows['price']);
}
print_r ($test);
No you should be able to reference by field like so:
echo $test['cage_doors'];
// returns 1800
Is the column 'field' unique? If so, do this:
while($rows=mysqli_fetch_assoc($result)){
$test[$rows['field']] = $rows['price'];
}
This will set the key in the array to the field, i.e.
Array('cage_doors' => 1800)
If its not unique, I recommend doing something like:
while($rows=mysqli_fetch_assoc($result)){
$test[$rows['field'] . '_' . $rows['pricing_id']] = $rows['price'];
}
This will set the key in the array to the field and the pricing id, which makes it unique, i.e.
Array('cage_doors_2' => 1800)
Hello stackoverflow people, can you help me with this error,
Warning: array_combine() expects parameter 2 to be array, null given
in
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
$row[] = mysql_fetch_array($check);
$check = array_combine($row[0], $row[1]);
Cant understand what is wrong with it. Ive tried doing [] this so it would be array but still nothng. My array looks like this:
Array
(
[0] => TVS darbai
[user_task_types] => TVS darbai
[1] => 14
[user_task_types_id] => 14
)
I want to connect user task type id with user task types it would look like [14] => TVS darbai
$row[] = appends to an array. Your array now looks like:
array(
0 => array(...)
)
As you see, there's no [1]. You probably just want $row = mysql_fetch_array(..), then $row[0] corresponds to the first column and row[1] to the second. It would still not make sense to array_combine those two columns though.
You probably want this:
$result = mysql_query(...);
$check = array();
while ($row = mysql_fetch_array($result)) {
$check[$row[0]] = $row[1];
}
You no need to combine. $row gives your output
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
while ($row = mysql_fetch_array($check) )
{
echo $row;
}
I have a foreach loop that goes through a list of items. For each of these items, I have a while loop that grabs data out of a database.
$output = array();
//$reference is a multidimensional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
array_push($output[$color], $person);
}
}
So this loops through, the first time searching 'red', and finding 5 people in the fake table who like red. Next, 'blue', where it finds 1 person, and then 'green' where it finds 3.
If I look at the individual results, my first array has "red, blue, green" and my second array has these lists of names.... I just don't know how to add them into an array together.
I'm trying to build an array like this:
Array
(
[Red] => Array
(
[0] => John
[1] => Sally
[2] => Bob
...
)
[Blue] => Array
(
[0] => Luke
)
[Green] => Array
(
..etc...
)
I'm not using array_push correctly though - I'm getting an Warning: Illegal offset type error. What am I doing wrong?
It's been a while since I've worked with PHP, but I think you need to initialize each "color" array that you're going to push into. So...
$output = array();
//$reference is a multidimentional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
if (!array_key_exists($color, $output)) {
$output[$color] = array();
}
array_push($output[$color], $person);
}
}
Try changing:
array_push($output[$color], $person);
Into:
$output[$color][] = $person;
From the manual on array_push:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
I have this table:
Username, Fan, Count
1danny22, katana1973, 2
bob, rita, 2
mattyhacky, hayley, 2
mattyhacky, dickie1eye, 1
mattyhacky, xxjodiexx, 1
And I want to load it into an associative array which would looks like this:
Array (
[1danny22] => Array (
[katana1973] => 2,
),
[bob] => Array (
[rita] => 2,
),
[mattyhacky] => Array (
[hayley] => 2,
[dickie1eye] => 1,
[xxjodiexx] = > 1,
)
)
$strSQL = "SELECT username, fan, count( * ) AS intCount
FROM fan
GROUP BY username, fan
ORDER BY username, intCount DESC";
$strResult = mysql_query($strSQL);
while($objRow=mysql_fetch_object($strResult))
{
code should go in here!!!
}
But I'm really struggling.
Can someone please help?
Many thanks
TheBounder.
Easy peasy...
$sql = "SELECT Username, Fan, Count FROM datatbl";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res))
$data[$row->Username][$row->Fan] = $row->Count;
I should explain further. What your doing here is creating a base index for your data array that uses the common associate Username as the index. Within that index you have another array, this using the Fan as the index and the value of each item being the count. My above example is the easiest way to build the structure that you are looking at.
Now if you run a print_r on the array you will see the structure you defined.
assuming your initial array is in $data, with for instance
$data[0] = array("1danny22", "katana1973", 2);
you could go with
$output = array();
foreach($data as $line) {
$output[$line[0]][$line[1]] = $line[2];
}
You could build it programmatically from the returned result... bare bones example:
$ar = array();
$result = mysql_query("SELECT * FROM your_table");
while ($ar[] = mysql_fetch_assoc($result));
print_r($ar);