Jump to content

Basic "Scope" Question


suttercain

Recommended Posts

Hi guys,

 

I am running the following code:

<?php require_once ('get_connected.php'); ?>
</head>

<body>
<?php
$results = mysql_query("SELECT * FROM typeOfCalls ORDER BY type") or die(mysql_error());
while ($row=mysql_fetch_assoc($results)) {
echo "<a href='?id=".$row['typeId']."'>".$row['type']."</a>";
echo "<br>";
}

if (isset($_GET['id'])) {
$timestamp = date('Y-m-d H:i:s');
$sql = "INSERT INTO totalCalls (typeId, dateInfo) VALUES ('".mysql_real_escape_string($_GET['id'])."', '$timestamp')";
$into = mysql_query($sql) or die(mysql_error());
if ($into) {
echo "You just added a " .$row['type']. " phone call."; } }
?>

 

My question is, how do I get the scope of the first SELECT statment into the last if statment that attempts to echo "You just added a " .$row['type']. " phone call."

 

The reason I ask is because right now the browser outputs "You just added a phone call" when I actually need it to echo "You just added a " .$row['type']. " phone call."

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/53751-basic-scope-question/
Share on other sites

<?php require_once ('get_connected.php'); ?>
</head>

<body>
<?php
$results = mysql_query("SELECT * FROM typeOfCalls ORDER BY type") or die(mysql_error());
while ($row=mysql_fetch_assoc($results)) {
echo "<a href='?id=".$row['typeId']."'>".$row['type']."</a>";
echo "<br>";
     $rows[$row['typeId']] = $row['type'];
}

if (isset($_GET['id'])) {
$timestamp = date('Y-m-d H:i:s');
$sql = "INSERT INTO totalCalls (typeId, dateInfo) VALUES ('".mysql_real_escape_string($_GET['id'])."', '$timestamp')";
$into = mysql_query($sql) or die(mysql_error());
if ($into) {
echo "You just added a " .$rows[$_GET['id']]. " phone call."; } }
?>

Link to comment
https://forums.phpfreaks.com/topic/53751-basic-scope-question/#findComment-265657
Share on other sites

No its a regular array.

 

What was happening is that your original code would of printed the very last result taken from that query. With what I posted there for you since you are using the typeid to add data is made that the key into a new array. Than all that was needed to be done was use the ID from the GET data on the Rows array to display that value.

Link to comment
https://forums.phpfreaks.com/topic/53751-basic-scope-question/#findComment-265666
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.