Solved

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

Posted on 2016-09-30
5
38 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now