PDA

View Full Version : help in understanding this php code


rocky86
08-01-2007, 07:21 AM
Right I am told to understand this part of the code before I can continue doing my code the problem is I don't noe what the code is doing can explain it to me thx:


<?php

$sqlconnect=mysql_connect("localhost","hyperian_track","gsmtrack");
if(!$sqlconnect)
die(mysql_error());
mysql_select_db("hyperian_track", $sqlconnect);

$fetched=mysql_query("SELECT * FROM location");

while($row = mysql_fetch_array($fetched))
{
$temp[]=$row[uname];
}

$names=sizeof($temp);
$report="total=$names&";

foreach($temp as $list)

$report.="name".$names--."=".$list."&";


echo $report;

mysql_close($con);


?>





Especially this part $report.="name".$names--."=".$list."&";

evride
08-01-2007, 08:39 AM
ok

$sqlconnect=mysql_connect("localhost","hyperian_track","gsmtrack");
if(!$sqlconnect) or die(mysql_error());

this tries to connect to a mysql server at localhost with the username of hyperian_track and password of gsmtrack. if it doesn't connect it outputs and error.

mysql_select_db("hyperian_track", $sqlconnect);
using the connection $sqlconnect it with open database "hyperian_track".

$fetched=mysql_query("SELECT * FROM location");
creates a sql query to select all entries from the table named "location" that is inside database "hyperian_track"

while($row = mysql_fetch_array($fetched))
{
$temp[]=$row[uname];
}

it creates a while statement that with loop through all mysql entries that were obtained from the querying until there are none left. the entries are then add to the array row. the stuff inside the while statement needs another paragraph

mysql database tables have columns. those columns make it so your data is organized. columns are labeled. in this example theres a column named uname. then all values from the column uname are added to the newly created array $temp.

$names=sizeof($temp);
the variable $names equals the length of the array $temp

$report="total=$names&";
not much to explain here.

i think you're missing something in the next set of lines, but i may be wrong.
i think it should be this:


foreach($temp as $list){
$report.="name".$names--."=".$list."&";
}

this will loop through all values of $temp and concatenates all variables and strings to the end of $report each time it is looped. so...
it concats "name", then the value of $names minus one each loop, "=", then the next username from the array $temp, and finally "&".

after the loop it echoes report which will be quite long.
echo $report;

then you close the database to save resources, which in your example was closed incorrectly. it would be
mysql_close($sqlconnect);
or
mysql_close();

then the closing tag

rocky86
08-01-2007, 10:07 AM
Thx for explaining right I am working on a php code there is require use the same logic to send the data to actionscript for display for my case I am gona send the uid and uname to actionscript so I have this code done I don't know if it is correct but pls help me check thx:

<?php
$temp="";




$sqlconnect=mysql_connect("localhost","hyperian_track","gsmtrack");
if(!$sqlconnect)
die(mysql_error());
mysql_select_db("hyperian_track", $sqlconnect);


if($_GET['postal']))

$postalcode=substr($_GET['postal'],0,2); //will return from 0 position 2 characters

$resultpostal=mysql_query("SELECT location.uname,location.uid FROM location,districts WHERE districts.districtno = '".$postalcode."' AND
location.lat BETWEEN districts.startlat AND districts.endlat AND location.lng BETWEEN districts.startlng AND districts.endlng");

if($row = mysql_fetch_array($resultpostal))
{
$temp[]=$row[uid];
$temp1[]=$row[uname];
}



foreach($temp as $result)
foreach($temp1 as $result)

$report ='&uid='.$result[0].'&uname='.$result[1];



echo $report;



}//end of if loops



mysql_close($con);




This part of the code does it send the uname and uid to actionscript?
if($row = mysql_fetch_array($resultpostal))
{
$temp[]=$row[uid];
$temp1[]=$row[uname];
}



foreach($temp as $result)
foreach($temp1 as $result)

$report ='&uid='.$result[0].'&uname='.$result[1];



echo $report;

rocky86
08-02-2007, 06:40 AM
Why is my uname and uid undefined when I run the output on actionscriptS? It should show the uname and uid but instead it show me undefined anyone noe?
my code:

PHP Code:
<?php

$sqlconnect=mysql_connect("localhost","hyperian_track","gsmtrack");
if(!$sqlconnect)
die(mysql_error());
mysql_select_db("hyperian_track", $sqlconnect);

if($_GET['postal']))
$postalcode=substr($_GET['postal'],0,2); //will return from 0 position 2 characters
$resultpostal=mysql_query("SELECT location.uname,location.uid FROM location,districts WHERE districts.districtno = '".$postalcode."' AND
location.lat BETWEEN districts.startlat AND districts.endlat AND location.lng BETWEEN districts.startlng AND districts.endlng");
if($row = mysql_fetch_array($resultpostal))
{
$temp[]=$row[uid];
$temp1[]=$row[uname];
}

foreach($temp as $result)
$report ='uid'."=".$result."&";
foreach($temp1 as $result1)
$report ='uname'."=".$result1."&";

echo $report;
mysql_close($con);


}
?>

rocky86
08-02-2007, 04:45 PM
Somebody explain the bottom section of code to me plss

<?php

$sqlconnect=mysql_connect("localhost","hyperian_track","gsmtrack");
if(!$sqlconnect)
die(mysql_error());
mysql_select_db("hyperian_track", $sqlconnect);

if($_GET['postal']))
$postalcode=substr($_GET['postal'],0,2); //will return from 0 position 2 characters
$resultpostal=mysql_query("SELECT location.uname,location.uid FROM location,districts WHERE districts.districtno = '".$postalcode."' AND
location.lat BETWEEN districts.startlat AND districts.endlat AND location.lng BETWEEN districts.startlng AND districts.endlng");

$counterx=0;
while($row=mysql_fetch_array($resultpostal)){
foreach($row as $col_value){
$temp[$counterx]=$col_value;
$counterx++;
}

$report ='uid'."=".$temp[0]."&";
$report .='uname'."=".$temp[1]."&";



echo $report;
mysql_close($con);


}
?>



I only want to noe what is this part of the code doing:

$counterx=0;
while($row=mysql_fetch_array($resultpostal)){
foreach($row as $col_value){
$temp[$counterx]=$col_value;
$counterx++;
}

$report ='uid'."=".$temp[0]."&";
$report .='uname'."=".$temp[1]."&";



echo $report;

evride
08-07-2007, 08:25 AM
its undefined because you didn't change your code. you need a & before the variable name