I got this code and need to sort the output by name:
$output = array();
while($res = mysql_fetch_assoc($res_handle))
{
$output[$res['_name']][] = $res['modell'];
}
Could anyone help me ? Thanks.
can you please try and let me know if this helps
ksort($output);
You can sort the table using several functions that php provide
or you can modify your sql query add the ORDER BY name
Use ORDER BY name in select query as follow :
SELECT * FROM Table_Name ORDER BY name
Thanks...
best way is use sorting in query, for example:
SELECT _name, modell FROM table_name ORDER BY _name ASC, modell ASC
This sort everything from A to Z. If you need results to sort from Z to A use DESC.
To sort the results in the field can be used, for example:
asort($output);
Overview table as sort fields:
http://php.net/manual/en/array.sorting.php
Simply you can use ORDER BY in Query
SELECT * FROM table_name ORDER BY column_name;
OR
Use asort in PHP array
asort($output[$res['_name']]);
Example
<?php
$name_array = array("0"=>"cc", "1"=>"aa", "Joe"=>"bb");
asort($name_array);
print_r($name_array);
?>
Result
Array ( [1] => aa [Joe] => bb [0] => cc )
Refer http://www.w3schools.com/php/php_arrays_sort.asp
Related
I want to fetch some info from my database, and choose which of the info in the string, I want to display.
--- What I know so far ---
This code is showing me NULL value:
$ticketis = mysql_query("SELECT `module` FROM games ORDER BY `id` DESC LIMIT 1,1");
$showticket = mysql_fetch_assoc($ticketis);
$rest = mb_substr($showticket,0,30);
var_dump($rest);
AND this code is showing me the data-string I want to cut out specific data from:
$ticketis = mysql_query("SELECT `module` FROM games ORDER BY `id` DESC LIMIT 1,1");
$showticket = mysql_fetch_assoc($ticketis);
var_dump($showticket);
This is the return value of the above code:
array(1) { ["module"]=> string(11) "0.435264836" }
What I want is to cut out is the numbers and display these: 0.435264836
Anyone got a clue what I can do to make that happen?
Thank you!
Use this $showticket['module']
mysql_fetch_assoc returns an array, not just a single value. Since you're using mysql_fetch_assoc you get an associative array indexed by the column name, hence the ['module']. If you would use mysql_fetch_row you'd get a numeric index and thus have to use $showticket[0].
But please don't forget that all the mysql_* functions are deprecated and will be removed in future versions of PHP!
Thanks for your help. It seemed I had to use print_r to get the pure numbers from the string. My code ended up like this, combining Remo and FirstOne's suggestions. Thanks.
$ticketis = mysql_query("SELECT `module` FROM games ORDER BY `id` DESC LIMIT 1,1");
$showticket = mysql_fetch_assoc($ticketis);
print_r($showticket['module']);
SELECT * FROM (`poll`) WHERE `poll_id` IN ('6,10,5,9,1') AND `poll_status` = '1'
I tried this query. I expected It would give result as I gave. But It sorting the result automatically in ascending. I need result as I give. Kindly give me a solution. Thanks in advance.
You can use FIELD in mysql:
SELECT *
FROM (`poll`)
WHERE `poll_id` IN (6,10,5,9,1)
AND `poll_status` = 1
ORDER BY FIELD(`poll_id`,6,10,5,9,1)
You have to separate your ids like that:
SELECT * FROM (`poll`) WHERE `poll_id` IN (6,10,5,9,1) AND `poll_status` = '1'
If I have a database with a field called items with '1,2,3,7,15,25,64,346' in it.
Is there a function to use the commas as a separator and take each number and start a new query and get info for that id? Like a foreach but in sql?
I can see it so clear but cannot put it in words...
You can use this query,
SELECT * FROM TABLE_NAME WHERE COLUMN_NAME in (1,2,3,7,15,25,64,346);
Yes, there is. You can use command IN
mysql_query( 'SELECT * FROM `table` WHERE `id` IN ( 1,2,3,4,5 );' );
WHERE - IN
$row = '1,2,3,7,15,25,64,346'; // $row = $field['value']
$arr = explode(',',$row);
foreach($arr as $a) {
// write your query
}
You can use the query SELECT * FROM table_name WHERE id IN ( 1,2,3,4,5 ) to find data having id's.
No need to separate the values. This will give you the desired result.
There is so way to do a "SQL foreach". Do it using PhP
Also, having such fields means your database is not normalized. You should give a look at relational database normalization, otherwise the bigger your database will be, the bigger your problems will be.
It seems you want data like this where 'source' is table having the column
SELECT target.* FROM target
LEFT JOIN source ON FIND_IN_SET(target.id, source.ids_row)
My query is
SELECT *
FROM `wp_patient_bill`
WHERE `bill_code` !=''
AND `is_billed` = 'Y'
AND `charged` = 'N'
AND `id` IN (97,419,631,632,633,422,635,421,35,799,60,423)
And I want the record array will be sort according to my list i.e. ID
'97,419,631,632,633,422,635,421,35,799,60,423'
. I am using php. Please suggest me the optimize way to do it.
Here is another solution:
SELECT *
FROM `wp_patient_bill`
WHERE `bill_code` !=''
AND `is_billed` = 'Y'
AND `charged` = 'N'
AND `id` IN (97,419,631,632,633,422,635,421,35,799,60,423)
ORDER BY FIELD(`id`,97,419,631,632,633,422,635,421,35,799,60,423)
Reference: SQL order by sequence of IN values in query
Assuming your list of IDs is in an array
Run the query
Loop through the results, storing each row of results in an associative array keyed on the ID $data["{$row['id']}"] = $row;
Loop through your list of IDs, and use the ID to key into the associative array to get the data. This will be in the order of the IDs you want.
Add an "ORDER BY ID" statement at the end of the query.
I am wondering if there is a way to select part of a column in mysql. For example, I want to select all statistics from my table that end in "_sep_2012". This would give me access to 1_sep_2012, 2_sep_2012, 3_sep_2012 etc...Is there a way to do this?
<?php
$query=mysql_query("SELECT %_sep_2012% FROM statistics WHERE id = '$id'");
$array = mysql_fetch_row($query);
$monthly_total = $array[0];
echo "$monthly_total";
?>
Any assistance would be greatly appreciated. Thanks!
According to your question it should be possible to do something like:
SELECT * FROM statistics WHERE datecolumn LIKE '%_sep_2012'
Not saying it's nice but it should get the data you need. You can add GROUP BY clauses to do a count or sum to get your total like:
SELECT datecolumn, SUM(amountcolumn) FROM statistics WHERE datecolumn LIKE '%_sep_2012' GROUP BY datecolumn
Write this in PHP:
for($i=1;$i<32;$i++)
printf("`%d_sep_2012`, ", $i);
Try this
SELECT * FROM statistics WHERE datecolumn LIKE '%_sep_2012' and id = '$id'