Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-30
5
Medium Priority
?
66 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 23

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 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 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 23

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

927 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