Jump to content

Assigning Specific Array Keys To Seperate Variables


dinita

Recommended Posts

is it possible to store the keys of an array in to variables?

 

currently my code looks like this:

function getanswers()
{
$sql    = mysql_query("select answer FROM answers WHERE question_ID= 1 ");
while($row    =mysql_fetch_row($sql))
{
 //print_r (array_keys($row));
 $ans1 = $row['0'];
 $ans2 = $row;
 $ans3 = $row;
 $ans4 = $row;

 echo $ans1 . $ans2.  $ans3. $ans4 ;
}
}

 

and all I get are undefined offset notices what am i doing wrong? I'd appreciate any advice

the output looks like this:

 

The ‘Sea Swallow’ is an alternative name for which bird?

Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29

 

Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30

 

Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31

Seagull

Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29

 

Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30

 

Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31

Penguin

Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29

 

Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30

 

Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31

Tern

Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29

 

Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30

 

Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31

Cormorant

 

 

Just in case it helps when I call the array keys they show this: Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 )

 

Thanks in advance to anyone who can help!

Link to comment
Share on other sites

here is the full code

 

//functions
function get_id( $table)
{
$sql    = mysql_query("select * FROM $table") ;
while ($row    =mysql_fetch_array($sql))
{
 return $row['ID'];
}
}
function getquestions($id)
{
$sql    =mysql_query("select text FROM questions WHERE quiz_ID =$id ");
while($row   = mysql_fetch_row($sql))
{
 return $row ;
}
}
function getanswers()
{
$sql    = mysql_query("select answer FROM answers WHERE question_ID= 1 ");
while($row    =mysql_fetch_row($sql))
{
 print_r (array_keys($row));
 $ans1 = $row['0'];
 $ans2 = $row['1'];
 $ans3 = $row['2'];
 $ans4 = $row['3'];

 echo $ans1 . $ans2.  $ans3. $ans4 ;
}
}
//Connect to Database
$con = mysql_connect("localhost","dinita","8888888");
if(!$con)
{
die('Could not connect: '.mysql_error());
}
else
{
// SELECT DATABASE
mysql_select_db("quizCreation", $con);
///Actual code
$quizid = get_id("quizName", "ID");
$questionid = get_id("questions","quiz_ID");
if ($quizid == $questionid)
{
 $question = getquestions("1");
 echo $question[0];
 getanswers(1);

mysql_close($con);
}
}

Link to comment
Share on other sites

thanks for all your help, but changing the numbers from strings to a number makes no difference, I still receive the same Notices.

 

I understand that its probably bad practice to use sequentially numbered variables but I haven't really been having much luck with arrays, I've only been learning php for 2 weeks, so please excuse my lack of knowledge. As I tried to explain I'm trying to understand how to call each part of the array, at the moment this is causing undefined notices.

 

maybe it would help if I explained what I was trying to do, this file will be sending variables to dynamic text fields in flash that will display the questions and answers from my database.

Link to comment
Share on other sites

That was a general tip about correct code, your code is still not written right because you're selecting 1 column and expecting 4.

 

You want something like:

 

$sql    = mysql_query("select answer FROM answers WHERE question_ID= 1 ");
$answers = array();
while($row      =mysql_fetch_row($sql))
{
 $answers[] = $row[0];
}
echo "The answers are: " . implode(', ', $answers);

-Dan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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