Solved

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

Posted on 2016-09-30
5
62 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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