1

I am stuck in writing a PHP function. What I need to do is pass on about 10 - 12 ID into an array and then sort them by name in mysql database and then print some statements. I got this far where I manually add an ID number and it prints the information I need but at this point I have to sort the data before using those ID numbers.

Here is what I have done so far.

<?php
function print_software_info($id) {
  $appinfo = "SELECT * FROM `appinfo` WHERE `id` = ".intval($id);
  $rt = mysql_query($appinfo) or die(mysql_error("Could not retrieve database information"));
  echo $rt['name'];
  echo $rt['image'];
}
?>
<?php 
print_software_info(217);
print_software_info(179);
print_software_info(8);
?>
3
  • actually i didnt get you what you exactly want.? please explain more. Commented Dec 14, 2010 at 5:21
  • I have a table with following fields. 1) ID 2) Name 3) Image_location 4) About. What I need to do is pass on ID values in an array and sort them by name and print information about them Commented Dec 14, 2010 at 5:23
  • Glad to say that thin function's design is terrible. Against all rules Commented Dec 14, 2010 at 6:26

1 Answer 1

1

with

$ids = array(13, 14, 250, ... );

First (optional for now, but keep in mine) make sure they're all integer (sanitize)

$ids = array_map('intval',$ids);

You need to build a query like this

$q = "SELECT * FROM `appinfo` WHERE `id` IN (". implode(',',$ids) ." ) ORDER BY name";

$result = mysql_query($q);

Then go through each rows..

while ($row = mysql_fetch_object($result)) {
    echo $row->name;
    echo $row->image;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is more like it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.