Forum Moderators: coopster
I couldn't make above code work, maybe somebody help me understand :
The originally code is something like :
-----------------------------------
//( $result is some mysql query, not important
while(list($id, $item_1, $item_2))= mysql_fetch_row($result)){
echo "....";
}//end while
--------------------------------------
But I need to DYNAMIC construct the variables $item_x because at running time I don't know exactly how many $item_x are in program, so I tryed to put the, after I count them, in some variable like that ( with some FOR loop ):
$generated_variables = "\$item_1, \$item_2, \$item_3";
and, after, I tryed to run the WHILE but , doesn't work as expected
while ( list($generated_variables)=mysql_fetch_row($result)){
...
...
}//end while
some ideeas?
Thanks in advance,
Have you tried assigning the fetched result row to an array and process the array with a foreach [php.net] loop yet? Or is there a reason you need to use a list [php.net] construct? For example:
while ($row = mysql_fetch_row($result)) {
foreach ($row as $key => $value) {
echo "$key: $value<br />";
}
}
The reason used list() is I don't know well other option :)). One again, I try do DYNAMIC extract some info from MYSQL like that :
FIRST, I make the SELECT statment in some string :
$variabila="title_1,desc_1,title_2,desc_2";
( note that variables caled title_x and desc_x can be more or less, depends on program )
$result = "SELECT art_id,author,$variabila FROM art WHERE art_cat='$cat' AND art_subcat='$subcat' AND activ='1'";
$final_result = mysql_query($result);
AFTER that, I need to extract from $final_result with an WHILE loop that info, like that :
while(list($art_id,$author, ... )== mysql_fetch_row($final_result){
echo "the damn variables...";
}
BUT INSTEAD of ... in WHILE, I need some DYNAMIC created names of variables, exactly like in mysql_query statment, encapsulated in string $variabila
And I tryed to make this with some other string, caled $variabila2="\$title_1,\$desc_1";
but seems to not working with list().
---------------------------------------------------
In fact, the problem is I think to USE some variables encapsulated in a string, like that :
instead of list($one,$two,$three) I need to
list($string) where $string="\$one,\$two,\$three";
( doesn't work ) any ideea?
respect,
eddy
What you are trying to do is, you want to dynamically select records from a table (nothing new) and also dynamically determine which fields to select.
Since you don't know at 'compile' time which fields are selected you want a function with which you can access the 'dynamic' values per field for each record.
(I DONT KNOW THE CORRECT SYNTAX FOR PHP (yet...), so the following is purely pseudo-code)
FieldsToSelect = 'field1; field2; field2';
(Which is set at runtime, so it could contain anything as you mentioned).
Static portion:
Query = "Select " + FieldsToSelect + " from table SomeTable;"
Result = ExecuteQuery( Query);
For R := each record in result do
Record = Result[R];
For F := each field in FieldsToSelect do
ValueForRecord = Record[ FieldsToSelect[ F]];
DoSomething with ValueForRecord... Echo, print, whatever.
Next
Next
I dont know the correct syntax, but you can access FIELDS BY NAME from an sql result list.
You pass the name of the field and you get the value belonging to it.
Someone else on this forum definitly knows this, now they know what you want :)