Jump to content

Recommended Posts

Ok, so this seems kinda vague, cus normally thats what you do, however I am talking about a differnt kinds of output from a query... Here is what i am a talking about:

 

My Query Code:

$query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2;
$result = mysql_query($query,$which);
$num = mysql_num_rows ($result);


$id = mysql_result($result,$i,"id");
$storeid = mysql_result($result,$i,"storeid");
$tinypass = mysql_result($result,$i,"tinypass");
$taxsetting = mysql_result($result,$i,"taxsetting");
$gst = mysql_result($result,$i,"gst");
$pst = mysql_result($result,$i,"pst");
$hst = mysql_result($result,$i,"hst");
$canpar = mysql_result($result,$i,"canpar");
$cp_fuelcharge = mysql_result($result,$i,"cp_fuelcharge");
$cp_markup = mysql_result($result,$i,"cp_markup");
$fedex = mysql_result($result,$i,"fedex");
$fe_fuelcharge = mysql_result($result,$i,"fe_fuelcharge");
$fe_markup = mysql_result($result,$i,"fe_markup");
$dhl = mysql_result($result,$i,"dhl");
$dh_fuelcharge = mysql_result($result,$i,"dh_fuelcharge");
$dh_markup = mysql_result($result,$i,"dh_markup");
$tnt = mysql_result($result,$i,"tnt");
$tn_fuelcharge = mysql_result($result,$i,"tn_fuelcharge");
$tn_markup = mysql_result($result,$i,"tn_markup");

 

As you can see, my variables are the same as my table column names... now if this "project" goes thorugh I could have hundreds of columns, I would hate to have to define each one making my file even larger.

 

Is there a way to make it dynamically create the variables via an array or something

 

Theoriticaly something like this:

$query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2;
$result = mysql_query($query,$which);
$num = mysql_num_rows ($result);

(While loop here)
$Table_name_var = mysql_result($result,$i,"table_name");
(end while loop)

 

Possible?

extract?

 

<?php
$query = "SELECT * FROM settings WHERE storeid='store350' ";
$which = $handle_db2;
$result = mysql_query($query,$which) or die(mysql_error());
$num = mysql_num_rows ($result);

if ($num > 0) {
$data = mysql_fetch_assoc($result);
extract($data);
}
?>

extract?

 

<?php
$query = "SELECT * FROM settings WHERE storeid='store350' ";
$which = $handle_db2;
$result = mysql_query($query,$which) or die(mysql_error());
$num = mysql_num_rows ($result);

if ($num > 0) {
$data = mysql_fetch_assoc($result);
extract($data);
}
?>

 

Ok, would this make it so that I can use the column names as variables?

 

for instance if I changed my code to the above, would the variable $dh_markup exist with the value of the dh_markup column from my db?

well theres another way too.... you can try this:

 

foreach($array_results as $key => $val){

    $$key = $val;

}

 

so your column names (the associative index) will be variables that hold the values

 

That is what extract() does.

That is what extract() does.

 

well indeed... .but theres some advantage of using loop over extract().... you might already know it but anyways heres a link i found....:

 

http://davidwalsh.name/convert-key-value-arrays-standard-variables-php

 

besides lostnode wanted to know the other way too....i guess..!!

 

 

Thank you for the responses, I hadn't tested it out but I saw that somewhere in my code was a snippet that does that extract and it seems to work fine.  I will test it out when I have a chance.  As I have seen it work in another file, I am marking this solved.

 

 

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.