Page 1 of 1
php variables
Posted: Fri Mar 05, 2010 8:39 pm
by scotter86
Hi everyone, I'm having an issue with variables. what im trying to do is query a table in a database that has a list of id's for html <div>'s (along with other properties). I know what i want to do, but i cant seem to find the right syntax, something like....
$i=0;
$total=odbc_num_rows($consql);
for($i; $i<$total; $i++){
$divID[$i]=odbc_result($conSql,'divID');
}
my end result is to get a list of variables and their id like..
$div1=menuDiv;
$div2=bodyDiv;
$div3=footerDiv;
something like that. I hope that makes sense, TIA
Scotter
Re: php variables
Posted: Fri Mar 05, 2010 9:10 pm
by mikosiko
scotter86 wrote:Hi everyone, I'm having an issue with variables. what im trying to do is query a table in a database that has a list of id's for html <div>'s (along with other properties). I know what i want to do, but i cant seem to find the right syntax, something like....
$i=0;
$total=odbc_num_rows($consql);
for($i; $i<$total; $i++){
$divID[$i]=odbc_result($conSql,'divID');
}
my end result is to get a list of variables and their id like..
$div1=menuDiv;
$div2=bodyDiv;
$div3=footerDiv;
something like that. I hope that makes sense, TIA
Scotter
and your sql query is?
in odbc_result you can use the field name : odbc_result($conSql,"menuDiv") or the position of the field in the resulset ... p.e if menuDiv is the 1st field returned for your query... odbc_result($conSql,1)
Miko
Re: php variables
Posted: Fri Mar 05, 2010 10:02 pm
by scotter86
well my sql query is
Code: Select all
$myRowSQL = "SELECT * FROM divs'";
$ir=odbc_exec($dbCon,$myRowSQL);
$divName = odbc_result($ir,"divName");
i mean i dont have a problem getting the divName, but i guess it would be like an array im looking for. I want to pull all the divName fields and assign them to seperate variables. but the tricky part is i want it dynamic so if i were to add a new row to the database it would still work without having to go to the php code and make a new var and assign it. thats why i wanted the odbc_num_rows() and make that the max in a for statement. but i tried that earlier and the odbc_num_rows() would always generate -1. from what i read its a driver issue and im not sure how to fix that sooooo ideas?
Re: php variables
Posted: Fri Mar 05, 2010 10:09 pm
by mikosiko
Re: php variables
Posted: Fri Mar 05, 2010 10:17 pm
by scotter86
ty ty ty ty, exactly what i was looking for.
Re: php variables
Posted: Sat Mar 06, 2010 12:37 pm
by scotter86
ok, i hit a wall again. that solution is working, but i want an array that stores the different variables.
Code: Select all
<?php
$dbCon=odbc_connect('siteDB','','');
$sql = 'SELECT rowId FROM divs';
$rs = odbc_exec($dbCon,$sql);
$div=array();
while (odbc_fetch_row($rs))
{
for($j=1;$j<=odbc_num_fields($rs);$j++){
$div[$j]=odbc_result($rs,$j);
}
}
?>
if i do: echo $div[1]; i get all the results as one string. $div[2] and above are all empty. why are the values going in the array as one long string instead of individually?
Re: php variables
Posted: Sat Mar 06, 2010 1:31 pm
by mikosiko
scotter86 wrote:ok, i hit a wall again. that solution is working, but i want an array that stores the different variables.
Code: Select all
<?php
$dbCon=odbc_connect('siteDB','','');
$sql = 'SELECT rowId FROM divs';
$rs = odbc_exec($dbCon,$sql);
$div=array();
while (odbc_fetch_row($rs))
{
for($j=1;$j<=odbc_num_fields($rs);$j++){
$div[$j]=odbc_result($rs,$j);
}
}
?>
if i do: echo $div[1]; i get all the results as one string. $div[2] and above are all empty. why are the values going in the array as one long string instead of individually?
because your code is doing exactly that....
Code: Select all
for($j=1;$j<=odbc_num_fields($rs);$j++){
$div[$j]=odbc_result($rs,$j);
}
think... how many fields are you extracting for each row in your select?... in other words... which is the value of odbc_num_fields($rs) for each record?....
maybe.. what you want is (simplified):
Code: Select all
$j=1;
while (odbc_fetch_row($rs))
{
$div[$j]=odbc_result($rs,$j);
++$j;
}
or maybe
Code: Select all
<?php
$dbCon=odbc_connect('siteDB','','');
$sql = '[color=#FF0000]SELECT * FROM divs[/color]';
$rs = odbc_exec($dbCon,$sql);
$div=array();
$i=1;
while (odbc_fetch_row($rs))
{
for($j=1;$j<=odbc_num_fields($rs);$j++){
$div[$i][$j]=odbc_result($rs,$j);
}
++$i;
}
?>