Jump to content

Recommended Posts

Greetings,

I'm trying to determine if there's a better way to return results and place them into an array. Essentially, I'm just trying to write less code, and I'm curious if there's a better way to do this. An example will probably help. Today, I do it like this :

[code]
$query = 'SELECT id, desc FROM table';
$result = mysql_query($query) or die ( 'Error: Query: ' . mysql_error() );

// Place the results into an array
$id = Array();
$desc = Array();
while ($line = mysql_fetch_array($result)) {
   array_push($id, $line[0]);
   array_push($desc, $line[1]);
}

// Free the result
mysql_free_result($result);

// Assign the array to the smarty template
$smarty->assign('id', $id);
$smarty->assign('desc', $desc);
[/code]

The purpose here is to assign this to a smarty template where the following would be used :

[code]
<select name='select_box'>
   {html_options values=$id output=$desc}
</select>
[/code]

Any tips on reducing my line count on this?

Thanks!
[!--quoteo(post=350367:date=Feb 28 2006, 04:12 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Feb 28 2006, 04:12 PM) [snapback]350367[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Well, your query is probably going to fail, desc is a reserved word in SQL.

Other than that, I don't think that you can really reduce that any more than it already is.

Not sure what your hoping for.
[/quote]

Well, the query was bogus, so that's not an issue. :)

I was really just wondering if this was the best way to do this or not.. Seems like a lot of code for a simple function.. But then, I guess a larger, more complex program is nothing more than a lot of these simple functions strung together.. :)
No, you're doing it correctly. Of course, if you were using my framework the whole thing would go like this:
[code]$data = $db->getallcolumns('SELECT id, desc FROM table');

// Assign the array to the smarty template
$smarty->assign('id', $data['id']);
$smarty->assign('desc', $data['desc']);[/code]

But you'd have to have my framework now wouldn't you? :o

I'll share if you PM me (Disclaimer: PHP5 only, but mysql or mysqli both supported).
Well in my vocabulary the API is just the public methods, while the framework is the whole thing.

But yeah, it's 5-only because object orientation changed so much. I've thought about porting a version that will run on 4, but I haven't much looked into it yet. I didn't write a lot of PHP4, I didn't care for the language until 5 went stable.

I put some documentation online the other day, take a look if you like. [a href=\"http://mythgaming.net/php/docs/database/db.html\" target=\"_blank\"]http://mythgaming.net/php/docs/database/db.html[/a]
[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) [snapback]350509[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Well in my vocabulary the API is just the public methods, while the framework is the whole thing.
[/quote]

Ok, semantics.. :) Either way, it seems to be just a new buzzword for an old concept.. Kinda like AJAX is the new buzzword for javascript activates DHTML.. :)

[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) [snapback]350509[/snapback][/div][div class=\'quotemain\'][!--quotec--]
But yeah, it's 5-only because object orientation changed so much. I've thought about porting a version that will run on 4, but I haven't much looked into it yet. I didn't write a lot of PHP4, I didn't care for the language until 5 went stable.
[/quote]

I stuck with 4 because it seems to have a much larger install base. It's a little easier on me since I have to deal with servers that have 4 installed already and 5 probably isn't forthcoming for quite a while...

[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) [snapback]350509[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I put some documentation online the other day, take a look if you like. [a href=\"http://mythgaming.net/php/docs/database/db.html\" target=\"_blank\"]http://mythgaming.net/php/docs/database/db.html[/a]
[/quote]

Excellent, thanks. This helps out a bit...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.