I have a web form with checkboxes for Event Spaces.
The checkbox values are grabbed from one table in my database "facility_event_charges":
Let's say I check 5 and 6, "Audio Visual Equipment" and "Audio Visual Technician" and submit my form.
This will save the id(s) to a separate table that I have for that "Event Space"
Let's say my "Event Space" has an ID of 63. The lookup table now has two entries for my id since I checked 2 of the checkboxes:
All is good and saved.
Now, let's say I need to actually edit the charges associated with this space.
When I click edit, my webform renders the form fields BUT the checkboxes are empty!! Why? Because it is grabbing the data from my "facility_event_charges" table.
I need to compare this table data (array) with the data saved in my lookup table (array) to figure out which values are in common and render a check in those checkboxes.
Desired result in my edit space webform would have those checked
My checkbox "charges" array:
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
--
I can also generate the charges in this:
array(6) {
[0]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "1" ["type"]=> string(9) "Officials" } }
[1]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "2" ["type"]=> string(6) "Ushers" } }
[2]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "3" ["type"]=> string(19) "Additional Staffing" } }
[3]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "4" ["type"]=> string(16) "Special Lighting" } }
[4]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "5" ["type"]=> string(22) "Audio Visual Equipment" } }
[5]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "6" ["type"]=> string(23) "Audio Visual Technician" } } }
My lookup table array:
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
--
I've been unsuccessful in my attempts - tried php array_intersect
array_intersect_assoc
array_map('array_diff_assoc')
I need to loop through the checkbox charges array and find a match from the lookup table array, when it matches, mark the checkbox "checked".
Anyone have any working examples I could test out?
The checkbox charges array key is what would need to match the "facility_event_charges_id"
As per you array given above you can compare like this
for example Lets take $charge array as
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
take $lookup array as
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
You can compare both array like below
$charges = array(
1 => "Officials",
2 => "Ushers",
3 => "Additional Staffing",
4 => "Special Lighting",
5 => "Audio Visual Equipment",
6 => "Audio Visual Technician"
);
$lookup = array(
array("EventCharge" => array(
"facility_event_charges_id" => "5"
)
), array(
"EventCharge" => array(
"facility_event_charges_id" => "6"
)
)
);
$lookup = array_column(array_column($lookup, 'EventCharge'), 'facility_event_charges_id');
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $lookup)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
Edit
If array_column() is not supported than you can create array using loop like below and test
$arr=array();
foreach ($lookup as $key => $value) {
foreach ($value as $k => $v) {
$arr[]=$v['facility_event_charges_id'];
}
}
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $arr)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
This works...
But it will change based on how exactly you end up building the arrays... but that should only require a change in the if-then statement and possibly a change in the value of i (the index variable).
<html>
<head></head>
<body>
<form>
<?php
// assume you build this based on a sql query
// you don't need to use the primary key for the
// array index
$charges[0]="Officials";
$charges[1]="Ushers";
$charges[2]="Additional Staffing";
$charges[3]="Special Lighting";
$charges[4]="AV Equipment";
$charges[5]="AV Technician";
// your array of eventcharges
// select facility_event_charges_id from tbl where facility_event_spaces_id='63'
// loop thru the result of teh query make a simple array of your evencharge ids
// gonna hardcode mine out of laziness
// since php is typeless we can use strings to compare to ints
// directly as long as we use == and not ===
// and in_array() (used below) goes with loose comparison too
$ec[]="4";
$ec[]="5";
// now we have an array of our descriptions
// and an array of the already selected choices
// from the db data
for($i=0;$i<count($charges);$i++){
print('<input type="checkbox" name="charges" value="'.$i.'" ');
if(in_array($i,$ec)){
print("checked");
}
print(" > ".$charges[$i]."<br />");
}
?>
</form>
</body></html>
Hi you can use following code.i assumed that you have got $arr=array('5','6'); from database.so you can us as follows
<?php
$arr=array('5'=>'Audio Visual Equipment','6'=>'Audio Visual Technician');
$arr=array_keys($arr);//if you have associated array
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="ISO-8859-1">
</head>
<body>
<input type="checkbox" name="charges" value="1" <?=in_array('1', $arr)?'checked=true':''?>> Officials<br>
<input type="checkbox" name="charges" value="2" <?=in_array('2', $arr)?'checked=true':''?>> Ushers<br>
<input type="checkbox" name="charges" value="3" <?=in_array('3', $arr)?'checked=true':''?>> Additional Staffing <br>
<input type="checkbox" name="charges" value="4" <?=in_array('4', $arr)?'checked=true':''?>> Special Lighting <br>
<input type="checkbox" name="charges" value="5" <?=in_array('5', $arr)?'checked=true':''?>> Audio Visual Equipment<br>
<input type="checkbox" name="charges" value="6" <?=in_array('6', $arr)?'checked=true':''?>> Audio Visual Technician<br>
</body>
</html>
***i am making a quiz app using php and mysql and i have to display random questions..but i have defined static indexes for the answers..my program shows random question by refreshing the window but the answers index dont refresh according to question as they are static
<?php
$con=mysql_connect('localhost','root','test123');
mysql_select_db("quiz",$con);
?>
<html>
<body>
<form method="get" action="result.php">
<?php
$query="select m_question,a,b,c,d,e from mcq ORDER BY RAND()";
$result=mysql_query($query);
$rows=mysql_num_rows($result);
$a=1;
for($j=0;$j<$rows;$j++)
{
echo "<br> $a." .mysql_result($result,$j,'m_question')."<br>" ;
echo "<input type = radio name=$a value='a'>" .mysql_result($result,$j,'a');
echo "<input type = radio name=$a value='b'>" .mysql_result($result,$j,'b');
echo "<input type = radio name=$a value='c'>" .mysql_result($result,$j,'c');
echo "<input type = radio name=$a value='d'>" .mysql_result($result,$j,'d');
echo "<input type = radio name=$a value='e'>" .mysql_result($result,$j,'e');
$a++;
}
?>
<br>
<br>
<input type = "submit" value ="submit result">
</body>
</html>
and this is my result file where i am comparing results .ans of 1 questions is "b" option and and 2nd is "a" and so on...
<?php
$result=0;
if ($_GET[1]=='b')
{$result++;}
if ($_GET[2]=='a')
{$result++;}
if ($_GET[3]=='b')
{$result++;}
if ($_GET[4]=='b')
{$result++;}
echo "your score::".$result;
?>
I'm not sure what you mean by 'Static indexes for the answers', but you should remember that your database schema should always reflect the data stored in such a way that you could answer any questions you might have just by looking at the data available. Since I don't exactly know what you have based only on your code (you don't specify your schema, for example), I'll assume we're starting from scratch - and you can compare what you have to what I come up with.
The Schema
First, you have two sets of data: Questions, and Answers. Since this is a quiz app, I'm presuming that the format of the quiz is 'Multiple Choice'; that is, each Question has two or more possible answers, and only one of them is correct.
This means that we should split the questions and the answers into two tables; lets call them 'questions' and 'answers' respectively.
Questions
In the 'questions' table, we need a few things:
A unique, numeric identifier for each question. This is needed to set up any relationships in the future, so that we can point to any particular question easily (sure the text strings in the question are probably unique already, but when you want to quickly point to a specific item in the table, computers are much faster at comparing and searching through numbers than they are at comparing and searching through text). This will be our primary key.
The question itself. This is rather obvious.
So far, our schema looks something like this (primary keys in bold):
questions
q_id
question
Note: 'q_id' (or whatever you name your primary key) should also be labeled as 'AUTO_INCREMENT'. When you enter each question into the database, you leave that field 'null', and it'll automatically assign it the next available number.
Answers
Now for the 'answers' table, we'll need some different things. We'll need to identify each answer individually (to be able to point to each one they can choose), the question the answer belongs to, and we'll also have to be able to identify which one is the correct answer.
We can do this by having, along with an 'answer id' primary key, a 'foreign key' referencing the 'q_id' column in the 'questions' table, since it's the primary key for that table and thus identifies each row (each question) uniquely. Then all we need is the answer itself, and whether or not it's the correct answer. The end result is something like this:
answers
a_id
q_id (foreign key: questions.q_id)
answer
correct (enum: 'yes', 'no')
This also allows for questions to have no right answer, or multiple right answers. You'd have to take care of your data to make sure it's in line with the quiz. If you want to restrict only one right answer per question, you can add a 'UNIQUE' index that encompasses both 'q_id' and 'correct'. In MySQL, it'd look something like:
UNIQUE INDEX (q_id, correct),
That would go inside of your CREATE TABLE statement, or into a separate ALTER TABLE statement.
Note: 'q_id' in 'answers' should not *use either AUTO_INCREMENT* or be a primary key. If you feel it is causing a bottleneck, put an index on it.
Example Data Set
To help see this schema in action, lets make a simple quiz:
questions
1, "How many inches are in a mile?";
2, "Why do people breathe air?";
3, "Why is Monty Python popular?";
answers
1, 1, "63,360", 'yes';
2, 1, "5,280", 'no';
3, 1, "Over a million", 'no';
4, 1, "Several Hundred", 'no';
5, 2, "So they don't die", 'yes';
6, 2, "Because it's cool", 'no';
7, 2, "Because it's polite", 'no';
8, 2, "So they can laugh at people who don't", 'no';
9, 3, "Because people find it funny", 'yes';
10, 3, "Because it is", 'no';
11, 3, "Because of the French", 'no';
12, 3, "It's popular?", 'no';
Querying in PHP
There are two sets of queries to worry about here: grabbing the questions, and grabbing the answers. I would like to first point out that in your code examples, you're using the old, antiquated 'mysql' set of functions ('mysql_query', 'mysql_result', and so forth).
This is HIGHLY discouraged, and you should learn to use either the newer 'Mysqli' library, or the more abstracted 'PDO' library. I will be using Mysqli.
Method 1: All at once.
The first way you could run the queries, is to select all the questions, and then all the answers at once, put them into a data structure (your own class, or just a nested array), and then randomize the order in which they're displayed. Here is some code showing this. The comments are worth reading:
<?php
//Connect to MySQL:
$con = new mysqli("localhost", "tynach", "505.2.0", "quiz");
//Grab the question list:
$get_questions = $con->prepare("SELECT q_id, question FROM questions");
$get_questions->execute();
$get_questions->bind_result($q_id, $question);
$questions = array();
while ($get_questions->fetch()) {
// We use $q_id twice because if you scramble the array, the first one
// changes. However, we still might want to keep track of the original
// question ID number.
// The reason we use it in that first spot to begin with is so that we
// know for sure that we can associate the answers to the correct
// questions.
$questions[$q_id] = array($q_id, $question, array());
}
// Grab the answer list:
$get_answers = $con->prepare("SELECT a_id, q_id, answer, correct FROM answers");
$get_answers->execute();
$get_answers->bind_result($a_id, $q_id, $answer, $correct);
while ($get_answers->fetch()) {
$questions[$q_id][2][$a_id] = array($a_id, $answer, $correct);
}
// Scramble the array, and print it out:
shuffle($questions);
var_dump($questions);
?>
The output to that is:
array(3) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
string(30) "How many inches are in a mile?"
[2]=>
array(4) {
[1]=>
array(3) {
[0]=>
int(1)
[1]=>
string(6) "63,360"
[2]=>
string(3) "yes"
}
[2]=>
array(3) {
[0]=>
int(2)
[1]=>
string(5) "5,280"
[2]=>
string(2) "no"
}
[3]=>
array(3) {
[0]=>
int(3)
[1]=>
string(14) "Over a million"
[2]=>
string(2) "no"
}
[4]=>
array(3) {
[0]=>
int(4)
[1]=>
string(15) "Several Hundred"
[2]=>
string(2) "no"
}
}
}
[1]=>
array(3) {
[0]=>
int(2)
[1]=>
string(26) "Why do people breathe air?"
[2]=>
array(4) {
[5]=>
array(3) {
[0]=>
int(5)
[1]=>
string(17) "So they don't die"
[2]=>
string(3) "yes"
}
[6]=>
array(3) {
[0]=>
int(6)
[1]=>
string(17) "Because it's cool"
[2]=>
string(2) "no"
}
[7]=>
array(3) {
[0]=>
int(7)
[1]=>
string(19) "Because it's polite"
[2]=>
string(2) "no"
}
[8]=>
array(3) {
[0]=>
int(8)
[1]=>
string(37) "So they can laugh at people who don't"
[2]=>
string(2) "no"
}
}
}
[2]=>
array(3) {
[0]=>
int(3)
[1]=>
string(28) "Why is Monty Python popular?"
[2]=>
array(4) {
[9]=>
array(3) {
[0]=>
int(9)
[1]=>
string(28) "Because people find it funny"
[2]=>
string(3) "yes"
}
[10]=>
array(3) {
[0]=>
int(10)
[1]=>
string(13) "Because it is"
[2]=>
string(2) "no"
}
[11]=>
array(3) {
[0]=>
int(11)
[1]=>
string(21) "Because of the French"
[2]=>
string(2) "no"
}
[12]=>
array(3) {
[0]=>
int(12)
[1]=>
string(13) "It's popular?"
[2]=>
string(2) "no"
}
}
}
}
I had more to write, but I'm about to go to a Superbowl party, so I'll edit this post with more info later.
I have a webpage which contains a form, like the following:
<Form action=”search.php” method=”POST”>
<fieldset>
<legend> Enter your search below </legend>
<textarea name=”query”> </textarea>
</fieldset>
</form>
The users text is read from query and search results are displayed using code in the following section:
if ($_POST['query'])
{
//Users query is read and results from a search API are displayed
}
The next thing that happens is that a list of synonyms are generated, stored in a multidimensional array called $synonyms which I have displayed in a left-hand-navigation bar, using the code shown below. $newline prints a newline (as the variable name suggests)
Example of a $synonyms array:
array(3)
{ [0]=> array(2)
{ [0]=> string(9) "chelonian"
[1]=> string(17) "chelonian reptile" }
[1]=> array(6)
{ [0]=> string(7) "capsize"
[1]=> string(11) "turn turtle"
[2]=> string(8) "overturn"
[3]=> string(9) "turn over"
[4]=> string(8) "tip over"
[5]=> string(9) "tump over" }
[2]=> array(4)
{ [0]=> string(4) "hunt"
[1]=> string(3) "run"
[2]=> string(9) "hunt down"
[3]=> string(10) "track down" }
}
Code used to output the array:
foreach ($synonyms as $test)
{ foreach ($test as $test2)
{
echo $test2.$newline.$newline;
}
}
What I want to happen is:
Turn each synonym into a clickable link..if the user clicks the synonym "capsize", the word capsize is sent to the section where the synonym(previously query) is read and processed into results.. ie. back to this section:
if ($_POST['query'])
{
// Synonym is read and results from a search API are displayed
// Previously 'query' was read here
// The cycle continues again
}
Any ideas or suggestions on this one would be great, thanks guys.
You should use GET in search form. Then list synonyms as shown below
foreach ($synonyms as $test)
{ foreach ($test as $test2)
{
// I used <br/> for newline
printf('%1$s<br/>', $test2);
}
}
Edit: And obviously, you should replace $_POST['query'] with $_GET['query']
I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.
I cannot seem to figure out how to send to a group in my list using mailChimp API
My code looks like this:
$conditions = array('field'=>'interests-1', 'op'=>'all', 'value'=>'myGroup');
$opts = array('match'=>'any', 'conditions'=>$conditions);
$retval = $api->campaignSegmentTest($listId, $opts);
But this yields bool(false). When fetched by
$retval = $api->listInterestGroupings($listId);
my list looks like this:
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(10) "myList"
["form_field"]=>
string(5) "radio"
["display_order"]=>
string(1) "0"
["groups"]=>
array(5) {
[0]=>
array(4) {
["bit"]=>
string(1) "1"
["name"]=>
string(9) "myGroup"
["display_order"]=>
string(1) "1"
["subscribers"]=>
int(1)
}
[1]=>
array(4) {
["bit"]=>
string(1) "2"
["name"]=>
string(9) "myGroup_2"
["display_order"]=>
string(1) "2"
["subscribers"]=>
int(1)
}
}
}
}
I have looked in the API documentation and searched for the answer, but cannot seem to figure it out. Grateful for help!
Looks like you are using the PHP wrapper - the first thing to do, like the examples included with it do, is to check for any errors by looking at $api->errorCode before messing with the $retval.
When you do that I'm certain you will see an error telling you that you haven't passed a proper "conditions" parameter since it is an array of arrays, not an array.