hi am building a quiz system and basically i have my questions in an array called $questions and answers in an array $answers i have created an interface in html php to add data questions and answers into this arrays
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
$ans1 = $_POST['ans1'];
$ans2 = $_POST['ans2'];
$questions = array();
array_push($questions,$question1,$question2);
$answers = array();
array_push($answers,$ans1,$ans2);
so to insert this values in a database this is what i do
$quest_count = count($questions);
for ($i=0;$i<=$quest_count;$i++)
{
$query = "INSERT INTO quiz (question,answer) VALUES ('$questions[$i]','$answers[$i]')";
$result = mysql_query($query);
}
so my problem is this the for loop should add two rows in the database since the questions array contains two values question1 and question2 but it only adds one row. can anyone help me out on this am sure its ('$questions[$i]','$answers[$i]') part that has a problem.
thanks
You're using the <= operator in your loop.
Change it to just less than:
for ($i=0;$i<$quest_count;$i++)
{
$query = "INSERT INTO quiz (question,answer) VALUES ('$questions[$i]','$answers[$i]')";
$result = mysql_query($query);
}
Also, make sure you properly sanitise your input strings.
Please use < than using <= in the for loop, this will ensure that the loop runs twice or the exact of count of question array.
Try this:
Here no need to count array values & for loop. Instead use foreach loop.
Here is the solution:
foreach ($questions as $key1 => $que, $answers as $key2 => $ans)
{
$query = "INSERT INTO quiz (question,answer) VALUES ('$que','$ans')";
$result = mysql_query($query);
}
-
Thanks
Related
I need to be able to check if certain values are present in title_id, but as title_id's are index's it won't allow me to do this. Is there a way to store the values of the index without storing the array itself?
I have been searching for this answer for a long time but anything I search pulls up answers to different questions.
$selects = (array) null;
$res = mysqli_query($datab, "SELECT title_id FROM selects") or die($datab->error);
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab;
}
///////////// part of a later query
if (!in_array($thisid, $selects)) {
$title[] = $datab;
$j++;
}
$selects is array of arrays, not array of title_id.
Try something like that:
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab['title_id'];
}
I use:
foreach($image_files as $index=>$file)}
to loop between pictures in a directory and list them in a webpage, every picture has three related column in the database (ID, NAME, DESCRIPTION) ... so I used
SELECT `NAME`, `DESCRIPTION` FROM `pic_info` WHERE `ID`= '$counter' ...
but i don't know how can I split the
$rows = mysql_fetch_array($result, MYSQL_ASSOC)
to 2 variable for each picture ... for example : for the first pic I need
$name[0] = pic1 and $description[0] = blahblahblah1 ...
$name[1] = pic2 , $description[1] = blahblahblah2 ...
using
foreach ($rows as $key => $value) {$$key = $value; }
just gives me a combined data like $name[0] and $description[0] that I don't know how can I split it to two variable ... and it'll be appreciated if someone show another ways to SELECT two column in a table and assign it to two variables .
It's a bit unclear what you're asking, but I think you might want something like this
$names = array();
$descriptions = array();
while($row = mysql_fetch_assoc($results))
{
$names[] = $row["NAME"];
$descriptions[] = $row["DESCRIPTION"];
}
Requires PHP >=5.5, but this might achieve what you're after
$names = array_column($rows, 'NAME');
$descriptions = array_column($rows, 'DESCRIPTION');
or simply build the arrays you want while looping through the resultset of your SQL query
I'm having an issue inserting an array into my database. When i tried implode, it all inserted all the arrays into one row. What i intend to achieve is to insert a single array into different rows.
In the array i have fields such as bag,shoes,cloths and i wish to insert into one table but different rows.
$subjectArray[] =$_POST['subject'];
$all_subjects_to_insert = array();
foreach ($subjectArray as $p){
if(!empty($p)) $all_subjects_to_insert = $p;
}
$all_subjects_to_insert = array_unique($all_subjects_to_insert);
$final = implode(',',$all_subjects_to_insert);
$query = "INSERT INTO #__sch_subject (subject) VALUES ('$final')";
I'll appreciate any help i get. Thanks.
if $all_subjects_to_insert is multidimensional array then what you are looking for is
foreach($all_subjects_to_insert as $value)
{
$query = "INSERT INTO #__sch_subject (subject) VALUES ('{$value}')";
mysql_query($query);
}
Reference
Thanks everyone. Its now working. The separator for the implode function was incorrect. Here is the working code
$subjectArray[] =$_POST['subject'];
$all_subjects_to_insert = array();
foreach ($subjectArray as $p){
if(!empty($p)) $all_subjects_to_insert = $p;
}
$all_subjects_to_insert = array_unique($all_subjects_to_insert);
$inserted_values = implode("'),('",$all_subjects_to_insert);
print_r($finalvalues);
$query = "INSERT INTO #__sch_subject (subject) VALUES ('$inserted_values')";
With the separator the insert command looks like this
$query ="INSERT INTO #__sch_subject (Subject) VALUES ('a'),('b'),('c')";
Thanks for the help. I really appreciate.
This question already has answers here:
Creating an array from a MySQL table
(2 answers)
Closed 10 years ago.
I am using PHPlot to make a graph.
I have an issue in generating the array from a MySQL table.
Basivally, I want to array is as follows:
$values = array($arrayx);
array('a',-3),
array('b',5),
array('c',7),
array('d',8),
array('e',12),
array('f',-6),
//);
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
Part of the code where I tried to retrieve values from table to feed the array
$query="SELECT * FROM tasks";
$result=mysql_query($query);
//using a for loop to add values to the array
while ($resource=mysql_fetch_array($result)){
$thedate = $resource["date"];
$title = $resource2["title"];
$innerarray = "array('.$thedate.', $title),";
}
$values = array($innerarray).");";
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
//Draw it
$graph->DrawGraph();
}
The way I'm doing the $innerarray and $values seems wrong. Can you please help me fix it?
Thank you
try replacing
$innerarray = "array('.$thedate.', $title),";
with
$innerarray = array($thedate, $title);
$new = array();
while(for condition ){
$new[] = '\''.thedate[$i].''\','.$title[$i].'\';
}
var_dump($new);
this an idea, you need to edit the code to make it working
I assume it is this that you want:
$sql="SELECT datefield, titlefield FROM tasks";
....
while (list($thedate,$thetitle) = mysql_fetch_array($result)) {
$values[] = array($thedate,$thetitle);
}
echo $values[0][0]; // will output your 1st date
echo $values[0][1]; // will output your 1st title
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
Likely a remedial question, but in all my days as a PHP user I have yet to encounter an answer. Basically, is there any way to grab a single field of a "mysql_query" as an array? For instance say I have the following:
$query = "Select id,first_name,last_name FROM people";
$result = mysql_query($query);
Is there any way to grab each (id, first_name, last_name) as individual arrays without iterating through the recordset? Can I say something like:
$ids = field_to_array($result['id']);
$first_names = field_to_array($result['first_name']);
$last_names = field_to_array($result['last_name']);
As I said, in the past I've always simply built the arrays as needed, but an existing method would be handy.
mysql doesn't have that as a native function. you could always write your own..
function mysql_convert_cols($dataset) {
foreach ($dataset as $row => $values) {
foreach ($values as $column => $value) {
$return[$$column][$row] = $value;
}
}
return($return);
}
$resultConverted = mysql_convert_cols($result);
$id=$resultConverted['id'];
$firstName=$resultConverted['firstName'];
I'm not sure why do you need this , but you can do it like this :
$resultArray = array();
while($row = mysql_fetch_array($result)){
$resultArray[] = array(
"id" => $row['id'],
"firstName"=>$row['first_name'],
"lastName"=>$row['last_name']
);
}
Check if values are in :
print_r($resultArray);
Then you can foreach or to do the for loop on values.