0

Edit : I need it to do only in Array Sort, As i am using procedure and sending it into json,

Here is my Table Structure,

SQL Fiddle

enter image description here

I want to display as

  1. alpha london
  2. alpha newyork
  3. beta delhi
  4. beta sydney

I mean, the second coloumn (name) should be in Ascending Order and the third coloumn (place) should be in Descending Order.

How i want is

  1. alpha london
  2. alpha newyowk
  3. beta delhi
  4. beta sydney

The Name should be in Asc Order and then to the right, the Place should be in Desc Order

What i have tried so far is

How can i do this ??

<?php
include ('conn.php');
$sql="SELECT * FROM test";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
while($rows=mysql_fetch_array($result))
{
foreach($result as $k=>$v)
{
echo $k;

}
}
?>

It displays the result as Invalid argument supplied to for each. What is the mistake i am doing and how can achieve my output

3
  • the way you want to display and the explanation you gave( I mean, the second coloumn (name) should be in Ascending Order and the third coloumn should be in Descending Order.... ) is confusing .please explain Commented Sep 30, 2014 at 7:06
  • I have updated my question in bold, kindly see it Commented Sep 30, 2014 at 7:10
  • if u want name in asc and place in desc dn desired output would b alpha newyork / alpha london/beta sydney /beta delhi Commented Sep 30, 2014 at 7:13

5 Answers 5

1

To fix the sorting, just add ORDER BY name, place

Then there's several more issues preventing this from working:

  1. You shouldn't call mysql_fetch_array outside the loop (this would discard the first row, alpha london in your example).
  2. You need to iterate over $rows, not $result (this is where the "invalid argument" error is coming from).
  3. You're not echoing the value; only the key. So you wouldn't be displaying the name and place at all; only the words "name" and "place".

You might want to do something like this to fix these:

<?php
include('conn.php');
$sql = "SELECT * FROM test ORDER BY name, place";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
  foreach ($rows as $k => $v) {
    echo "$k is $v. ";
  }
  echo "<br/>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

So why don't you do an order in your query itself like

SELECT * FROM test order by name;

Comments

0

You can do this only modifying query.

Use this query:

SELECT * FROM test ORDER BY name ASC, place DESC

Comments

0

For your error, you are using wrong variable as foreach. Replace this -

foreach($result as $k=>$v)
{
    echo $k;
}

with this -

foreach($rows as $k=>$v)
{
    echo $k;
}

Buy why are using foreach inside while loop. You can get your values like -

while($rows = mysql_fetch_array($result)) {
    echo $rows['name'].' '.$rows['place'].'<br/>';
}

and don't use mysql_* function. Use instead mysqli_*

And for your expected output, try using following query -

SELECT * FROM `test` order by name asc, place desc

Comments

0

This is the kind of thing you can fix using array_multisort. Try the below example (which I have not tested yet, but ought to work)

<?php
include ('conn.php');
$sql="SELECT * FROM test";
$result=mysql_query($sql);

$totals = array();
$row = array();
$places = array();
while($row=mysql_fetch_array($result)){
    $totals[] = $row
    $names[] = $row['name'];
    $places[] = $row['place'];
}
array_multisort( $names, SORT_ASC, $places, SORT_DESC, $totals );

// now you can use $totals, which is sorted as you want
print_r( $totals );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.