-1

I have itinerary/tour page that contain number of sections: header. description, body, guide_details, footer

I would like to let my client to set the order - which section will appear first, second, third...

So I have array from DB that contain the number of each section. The number id the section position

Table itin_oreder:

clientID     header    description  body  guide_details footer
   6           1            3        2          4          5

$query = "SELECT * FROM itin_oreder WHERE client= 6 ";
$res = mysql_query($query);

I also have array that contain the content of each section:

itinContent = array ("header" => $header, "description" => $description....)

How do I print the array itinContent in the order that the client set?

dWinder
  • 11,597
  • 3
  • 24
  • 39
Roi
  • 153
  • 1
  • 11
  • 3
    haven't you tried `asort`, or just simply google `sort arrays php`? – Kevin Jul 09 '18 at 07:45
  • 1
    `mysql_query()` -- what is that? Have you read its [documentation page](http://php.net/manual/en/function.mysql-query.php)? There is a big red warning that says ***"This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://php.net/manual/en/ref.pdo-mysql.php) extension should be used"***. – axiac Jul 09 '18 at 07:58
  • You need to create a `form` so the client can use that to sort the query, or use DataTables and they can sort it them self with a click of a button. – Anuga Jul 09 '18 at 08:09
  • @axiac - `mysql_query()` is the code to run sql queries and it's working on PHP 5.1 - the same the one i use – Roi Jul 09 '18 at 11:40

1 Answers1

2

What you are looking for call "uksort" (uksort in PHP-MANUAL)

Let say you have itin_oreder array with the order to the scheme. you can build comparison function by that global array.

This is sample code:

<?php

$init_order= array("clientID"=>6, "header"=>3, ...);
$initContent = array ("header" => $header, "description" => $description....)

function sortByInitOrder($a,$b)
{
        global $init_order;
        return ($init_order[$a] > $init_order[$b]);
}

uksort($initContent, 'sortByInitOrder');
echo print_r($initContent, true);

If you want to print only the array values:

foreach($initContent as $key => $value)
{
  echo "$value \n";
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Hi David, i read about uksort but dodn't understand how its should works and how to use it...can you please explain? – Roi Jul 09 '18 at 11:44
  • uksort is a PHP function that sort the given array by user-define function that operate on the array keys. As you can see in my example, I define the compare function according to the init_order array - taking the client order and rearrange the key of the initContent array accordingly. Did you try to run the example? (after setting you array) – dWinder Jul 09 '18 at 11:54
  • Thanks. Not yet, but i'm about to... – Roi Jul 09 '18 at 12:04
  • David, you code also print the $initContent key name (i.e: "header") how can i not print it? – Roi Jul 09 '18 at 13:42
  • Do you want to print only the value without the key? why? – dWinder Jul 09 '18 at 13:46
  • Because i only need to print the body and description on the screen. I don't need to print the words ` Array ( [description] => ` or ` [body] => ` – Roi Jul 09 '18 at 13:49
  • ok, i used foreach loop and fix it. thanks! – Roi Jul 09 '18 at 13:57
  • 1
    Done. Hope i did it right.... – Roi Jul 09 '18 at 16:36