suttercain Posted May 31, 2007 Share Posted May 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53751-basic-scope-question/ Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 <?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."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53751-basic-scope-question/#findComment-265657 Share on other sites More sharing options...
suttercain Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks Frost, that worked, but just to gain more from the code: How does that work? That's a multi-dimensional array right? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/53751-basic-scope-question/#findComment-265662 Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53751-basic-scope-question/#findComment-265666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.