Solved

Sort order issue from query/json string in DB to xml

Posted on 2016-09-30
5
56 Views
Last Modified: 2016-09-30
Hi,
Having a big issue with sort order when retrieving images from DB and exporting as xml.
In the DB the images are saved in a longtext column like this:
["5.jpg","11.jpg","10.jpg","6.jpg","21.jpg","2.jpg","20.jpg","4.jpg","19.jpg","9.jpg","14.jpg","8.jpg","1.jpg","23.jpg","16.jpg","18.jpg","7.jpg","15.jpg","22.jpg","17.jpg","3.jpg","13.jpg","12.jpg"]

Open in new window


When I run the query I am getting that sort order, but need them sequentially.

I'm attempting it by doing this:
// Process image url loop
$pics = json_decode($row->images);
natsort($pics);

$px = count($pics);
$feed->startElement('Images');
for($i=0; $i<$px; $i++)
{
$feed->startElement('Image');
$feed->writeElement('FileURL', url() . '/media/used_vehicles/' . $row->id . '/full/'. $pics[$i]);
$feed->endElement(); // End Image										
}
$feed->endElement(); // End Images

Open in new window

but my sort order is unchanged in the resulting xml

Any ideas?
0
Comment
Question by:tjyoung
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41823465
That's beacuse natsort ( and also natcasesort) mantains the idx/value relationship so if you use that idx like $pics[$i] you'll have back the original indexed value
(try a print_r to understand it)

You should loop into the sorted $pics using a foreach routine

foreach ($pics as $thispic)
{
  $feed->startElement('Image');
  $feed->writeElement('FileURL', url() . '/media/used_vehicles/' . $row->id . '/full/'. $thispic);
  $feed->endElement(); // End Image	 
} 

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 41823495
HI,
Thanks for the info. I tried running that but it didn't sort as hoped.
This is my adjustment in case I missed something:

// Process image url loop
$pics = json_decode($row->images);
								
$feed->startElement('Images');
foreach ($pics as $thispic)
{
  $feed->startElement('Image');
  $feed->writeElement('FileURL', url() . '/media/used_vehicles/' . $row->id . '/full/'. $thispic);
  $feed->endElement(); // End Image	 
}
$feed->endElement(); // End Images
								

Open in new window

0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 41823511
You still have to call natsort, and then use foreach
// Process image url loop
$pics = json_decode($row->images);
// now sort by natsort
natsort($pics);
								
$feed->startElement('Images');
foreach ($pics as $thispic)
{
  $feed->startElement('Image');
  $feed->writeElement('FileURL', url() . '/media/used_vehicles/' . $row->id . '/full/'. $thispic);
  $feed->endElement(); // End Image	 
}
$feed->endElement(); // End Images

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41823545
Thats awesome. Sorry I wondered if I needed that after the fact.
Really appreciate you taking the time. Saved me much pain :)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41823554
Glad to have helped you ;)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question