Getting data from hundreds of fields [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is more of a hypothetical, since I just want to use the most efficient way to go about this.
Say you have a page with over 100 textboxes, and a submit button that bring you to a next page and displays what you've written.
How would I go about only printing the textboxes that have been filled in?
I would know how to do this with a lot of if statements, or a very long switch statement, but perhaps there's a simpler way?
Thank you!

yes, there is.
simply call them numbering them.
for example you'll have textbox1, textbox2, textbox3...
in this way, on the next page you just have to build a for loop.
for ($index = 1; $index < 100; $index++) {
if (!empty($_REQUEST["textbox" . $index]))
{
echo "textbox number {$index} isn't empty!";
}
}
in this way you will get each box is full or not.

Something like
<?php
for($i=0; $i<100; $i++){
if(!empty($_POST['textbox'.$i]))
echo $_POST['textbox'.$i].'<br>';
}
?>
should work I guess.
I did not test it

if the name itself is not important i would give them the name txtbox[] (name="txtbox[]")
This way you can choose one name for all the boxes that belong together, have a specific array to loop through $_POST['txtbox'] . Then just echo them all
foreach($_POST['txtbox'] as $key => $tb) {
echo "<br>Box " . $key . ": " . $tb;
}

Give them names like txtbox_1 to txtbox_100
foreach ($_POST as $key => $value) {
if (is_integer(strpos($key, 'txtbox_')) && $value != '') {
echo "<br />$key = $value ";
}
}

Related

File name as variable+.txt [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
session_start();
$user = $_SESSION['username'];
if( isset($_POST['subm_btn']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
}
function incrementClickCount()
{ $count = getClickCount() + 1;
file_put_contents($user.".txt", $count);
}
User register on my site, then he click on button (name="subm_btn"). I want count clicks and add number of clicks in file with name "username.txt"
I guess you are looking for something like this:
$file=$user.'.txt';
incrementClickCount($file);
function incrementClickCount($file){
$count = getClickCount($file) + 1;
file_put_contents($file, $count);
}
function getClickCount($file) {
return (int)file_get_contents($file);
}
If you want the variable to be available inside a function you either make it global or pass it as an argument (which is better).
You define $user, and then access $user1. That would be my guess as to why it doesn't work. Also, using $file might be a better idea anyway.

PHP implode function in if and foreach condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my HTML Code
<input type="checkbox" value="fotoğraf" name="materyal[]"> Fotoğraf
<input type="checkbox" value="resim" name="materyal[]">Resim
<input type="checkbox" value="çizelge" name="materyal[]">Çizelge
<input type="checkbox" value="katlanabilir harita" name="materyal[]"> Katlanabilir Harita
There are checkbox and i'll take values with $_POST['materyal'] and then i want to write screen values with comma if values more than one. if checkbox values empty i don't write anything screen.
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
$materyal = $_POST['materyal'];
echo "İçerdiği extra materyaller ; ";
foreach ($materyal as $materyallist) {
foreach ($materyallist as $yenimateryal){
array_push($sonmateryal, $yenimateryal);
}
}
echo implode(", ", $sonmateryal);
}
This is my code. when i want to use implode in if contiditon, I take mistake.
How can i do
It seems like you just want to show the contents?
$sonmateryal = array();
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ";
foreach ($_POST['materyal'] as $materyallist) {
foreach ($materyallist as $yenimateryal) {
$sonmateryal[] = $yenimateryal;
}
}
}
echo implode(", ", $sonmateryal);
update according to given HTML
if (!empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ".htmlentities(implode(',', $_POST['materyal']));
}
Since you're posting the inputs as simple array's this will implode the results with a comma if the post-ed value isn't empty..

Count words from textarea , error switch [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a form with an input type number and a textarea, named valoare and textarea
I have 5 radiobuttons,each with name option and value 1,2,3,4,5
so lets say for first radio button I want to display the number of words from textarea.
This is the code:
if (!empty($_POST['textarea']) && !empty($_POST['valoare']))
{
$option = "option";
switch($option)
{
case 1:
if(isset($_POST['submit']))
{
$count =str_word_count($_POST['textarea']);
echo $count;
break;
}
}
}
what's wrong with this ? I don't see any result.
Thanks
That code is correct, but as you have manually set $option to the value of option, it is not 1 so the code in the case 1 statement does not run.
You should place the break; outside of the if statement though in case you add more options.
You can also add a default option at the end:
switch($option)
{
case 1:
if(isset($_POST['submit']))
{
$count =str_word_count($_POST['textarea']);
echo $count;
}
break;
default:
echo "Option is: " . $option;
}
By the way, if I understand you correctly, you probably want:
$option = $_POST['valoare'];
instead of:
$option = "option";
$option = "option";
switch($option)
{
case 1:
how can you switch option if option is already defined? if you define $option="option" $option will never be 1.

Trying to echo a variable ( $i) into another variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

Trying and failing to place looped content within a div

Basically here is the scenario:
I am trying to generate a test from a database. Each test has many questions(items) each item is of a certain type (multi-choice etc), each question has multiple answers attached to it.
(I'm not looking for somebody to tell me im going about it all wrong)
I currently, loop through my query results and print out every question there. Within that loop I execute a function (present_question) for every question. Within this function is a switch case, which identifies the item type. Within each case, I run another loop to print out all the answers attached to that question.
What I am trying to do, is insert each question into a unique <div> so that I toggle the display etc between each question as they select a button for each so 1,2,3,4 etc.
However, it would appear I am struggling with this. My questions all print out, however It appears they are not within individual <divs> as I tried testing it by changing some attributes of the class and its doing nothing. :(
Here is the main code covering the whole procedure:
main loop:
//loop through every question present in query results and run function to present the different question structures
while ($qs = mysql_fetch_assoc($get_questions))
{
$type = $qs['item_type'];
$item_id = $qs['item_id'];
$question = $qs['question_text'];
echo "<div class='q_center' id='q_$q_num'>"; // insert each question into its own div
echo "<h2>Question No.$q_num </h2><br>";
echo "$question <br>"; // print out actual question
present_question($item_id, $type);
$q_num ++;
}
present_question function
function present_question($ID, $type){
// grab all the answers attached to the question
$get_answers = mysql_query("SELECT * FROM answers_tb WHERE item_id='$ID'");
echo "<form>";
// switch for different structured questions. (switched by question type)
switch ($type)
{
case "1":
multi_choice($get_answers); // just working on this type at the moment
break;
case "2":
echo "this type hasnt been done yet";
break;
}
echo "</div>";
echo "</form>";
return;
}
function for multiple choice type
function multi_choice($get_answers){
while ($answers = mysql_fetch_assoc($get_answers))
{
$as = $answers['text_value'];
echo "<input type='radio' name='1' value='$as'>$as<hr />";
}
return;
}
hopefully you guys can help me.
I am a beginner, so I do apologise if my code is an abomination :D
thanks,
rough version (untested)
$i = 0; //declares the counter $i
//loop through every question present in query results and run function to present the different question structures
while ($qs = mysql_fetch_assoc($get_questions))
{
$type = $qs['item_type'];
$item_id = $qs['item_id'];
$question = $qs['question_text'];
$i++; // adds 1 to $i
echo "<div class='q_center' id='q_$q_num'>"; // insert each question into its own div
echo "<h2>Question No.$q_num </h2><br>";
echo "<div class=question" .$i. ">"; //appends $i to the div name creating question[$i]
echo "$question <br>"; // print out actual question
echo "</div>";
present_question($item_id, $type);
$q_num ++;
}
should make div's named question1, question2, etc

Categories