Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

19 Experts available now in Live!

Get 1:1 Help Now