Hy, can anyone help me, i have problem with my script..
if I input 4324 in input field nmber, i want the result like this :
4324
4342
4234
4243
4432
4423
3424
3442
3244
2434
2443
2344
this is my script :
<form name="a" method="POST" action="">
<table border="1" width="100%">
<tbody><tr>
<td height="38" align="center"><b>Number</b>
<input name="nmber" size="8.5" maxlength="4" type="text" value="<?php echo $_POST['nmber']; ?>"> <b>Buy</b>
<input name="buy" size="6" type="text" value="<?php echo $_POST['buy']; ?>"> <font color="#000000" size="2"><b>(x 1000)</b></font>
<input name="save" style="padding:7px;" value="Submit" type="submit">
</td>
</tr>
</tbody></table>
</form>
And this is my php script :
<?php
if(isset($_POST['save']))
{
$dataangka=$_POST['nmber'];
$databetnya=$_POST['buy'];
$rupiahkali=$databetnya*1000;
$dataangkasplit=str_split($dataangka);
$angka1=$dataangkasplit[0];
$angka2=$dataangkasplit[1];
$angka3=$dataangkasplit[2];
$angka4=$dataangkasplit[3];
$no=1;
$n=24;
for($i=1;$i<=$n;$i++)
{
?>
<tr align="center">
<td><?=$no?></td>
<td><input name="cek[<?=$i?>]" value="1" checked="checked" type="checkbox"></td>
<td><?php echo substr(str_shuffle("$dataangka"),0,$n); ?>
<input size="2" name="res[<?=$i?>]" value="<?php echo substr(str_shuffle("$dataangka"),0,$angka4); ?>" type="hidden">
</td>
<td><?=$rupiahkali?></b> <input size="2" name="bet[<?=$i?>]" value="<?=$rupiahkali?>" type="hidden"></td>
</tr>
<?php
$no++;
}
}
?>
I have already try with substr and str_shuffle but result not like what i want..
Please help me.. :(
Thank you so much..
You are trying to generate all permutation of length 4 using the string 4324.
The easiest way to generate all permutation (imho) is recursion. But it you can do it in iterative method too.
I would suggest you study the algorithm first and get a grip on recursion. A quick google search returned the following results
http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
How to print all permutations of String both iterative and Recursive way? VB.NET
Generate list of all possible permutations of a string
This does what you need:
function getCombinations(array $a)
{
switch (TRUE)
{
case !isset($a[1]):
return $a;
case !isset($a[2]):
return array(implode($a), implode(array_reverse($a)));
default:
$return = [];
foreach ($a as $k => $e)
{
$c = $a;
array_splice($c, $k, 1);
foreach (getCombinations($c) as $r)
{
$return[] = $e . $r;
}
}
return array_unique($return);
}
}
$s = '4324';
echo implode('<br>', getCombinations(str_split($s)));
Related
I don't have much experience with php that's why I hope someone can help me to solve the problem.
I am trying to extend my database with new files. I need to be able to type in four different values in my form and save the form with the typed in values. If I only use the first input-tag #inschriftennummer1, it works. My saved form shows me what I've typed in. But I need three more inputs in order to tag the document with 4 values. If I use the code like that, no input value is shown when I save the form.
Maybe someone has a solution :)
<body>
<form name="Formular" method="post" action="">
<table style="width: 100%;">
<tr>
<td style="width: 0%;">
</td>
<td style="width: 85%;">
<?php
if (isset($_GET["change"]))
{
$xml = simplexml_load_file("Inschriften/".$_GET["change"].".xml");
echo '<input type="hidden" name="inschriftennummerAlt" value="'.$xml- >Inschriftennummer.'"/>';
echo '<input type="hidden" name="change"/>';
}
else
{
$xml = simplexml_load_file("Inschriften/Null.xml");
}
/*Gibt zu einem String die jeweilige Hexadezimalform zurück*/
function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return str_replace("da", "", $hex);
}
/*Gibt zu einer Hexadezimalform den jeweiligen String zurück*/
function hexToStr($hex)
{
$string='';
for ($i=0; $i <strlen($hex)-1; $i+=2)
{
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
?>
<div class="main">
<div class="meta">
<table>
<tbody>
<tr>
<td colspan="2">
<?php
if (strlen($_GET["inschriftennummer"] == 0))
{
// echo "Bitte Inschriftennummer überprüfen.";
}
?>
</td>
</tr>
<tr>
<td class="I"><span class="label">Inschriftennummer</span>:</td>
<?php echo '<input type="hidden" name="inschriftvorher" value="'.$xml- >Inschriftennummer.'">'; ?>
<td class="II">
<input name="inschriftennummer1" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer2" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer3" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
<input name="inschriftennummer4" type="text" size="7" value="<?php echo str_replace("_", " ", $xml->Inschriftennummer);?>"/>
</td>
this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case
<?php
$ans=$_REQUEST['ans'];
$qes=$_REQUEST['qes'];
$ra=$_REQUEST['right_op'];
$count=0;
for($i=0; $i<count($ans); $i++)
{
echo "Question".$qes[$i]"<br>";
echo "Ans".$ans[$i]"<br>";
echo "Right Option".$ra[$i]."<br>";
if(isset($ans[$i]) == isset($ra[$i]))
{
$count++;
}
}
?>
when submit the first page, then it shows
Notice: Undefined offset: 1 in C:\xampp\htdocs\result.php on line 9
The first page where the data is posted.
<?php
$i=1;
$x=0;
$y=0;
$z=0;
do{
?>
<tr>
<td width="30"></td>
<td width="30" height="27"><?php echo "$i";?></td>
<td width="493"><?php echo $row_question['question']; ?>
<input type="hidden" name="q_id[<?php// echo $y; ?>]" id="q_id" value=" <?php echo $row_question['q_id']; ?>" />
<input type="hidden" name="qes[<?php echo $y; ?>]" id="qes" value="<?php echo $row_question['question']; ?>" /><input name="right_op[<?php echo $z; ?>]" type="hidden" id="right_op" value="<?php echo $row_question['right_op']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td height="59" align="right"><h3>A)</h3>
<h3>B)</h3>
<h3>C)</h3>
<h3>D)</h3></td>
<td><h3>
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="A" id="ans_0" />
<font color="#FFFFFF"><?php echo $row_question['op_a']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="B" id="ans_1" />
<font color="#FFFFFF"><?php echo $row_question['op_b']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="C" id="ans_2" />
<font color="#FFFFFF"><?php echo $row_question['op_c']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="D" id="ans_3" />
<font color="#FFFFFF"><?php echo $row_question['op_d']; ?></font> </label>
<br />
</h3></td>
</tr>
<tr>
<td height="17" colspan="3"><hr /></td>
</tr><?php
$i++;
$x++;
$y++;
$z++;
} while ($row_question = mysql_fetch_assoc($question)); ?>
What is wrong in this code. Please tell me in details.
Thank You.
Always use isset() before access
if (isset($ra[$i]) && isset($ans[$i]) && isset($qes[$i])){
// your code
}
or
array_key_exists($i, $ra);
or
It looks like you expect all arrays to be the same size, which is not the case for $ra which is snorter than other arrays, therefore
echo "Right Option".$ra[$i]."<br>";
causes the notice. Looks like you need to fix your $ra size.
$ra doesn't have as many members in the array as $ans does, so when you hit the 2nd iteration of the loop there's nothing in $ra[1] to display. You need to figure out why the length of $ra and $ans are different - without more information about what you're doing it's difficult for us to know.
As #Nikola indicates, you should always check to make sure there's something in the index you're accessing before trying to output it using isset(). Whether or not it's valid for there not to be something in $ra[1] when there is something in $ans[1] is something only you can answer.
Inferring a lot from your code, it looks as though $ra is intended to indicate the right answer. In that case there's only going to be one, so you should use $ra[0] instead of $ra[$i], which will always show the first element in the $ra array. Of course I may be misinterpreting the intent of your code, so YMMV.
I have a form such as the below:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/></td>
Now when I go to second page to look at the entries of the array, I do this:
<?php
$itemsBasket = array( );
$itemsBasket = $_POST['basket'];
echo "<h1>The Items Are...</h1><br>";
//print_r($itemsBasket);
foreach ($itemsBasket as $value)
{
if($value > 0){
echo $value . "<br>";
}
}
?>
This will print the value at the indexes of the array...but I need to store the name of the index so lets say the item is chocolate and value of 12. I want to extract that index name from array to store it in variable and then assign value to that variable...
Any way I can do that? right now I get only the value while iterating...
Thanks for help and sorry if question isn't clear I will help explain better if so...
UPDATE: this is the unexpected output....
whitethoab: Array woolthoab: 22 shemag: 22 undershirt: 1 serwalthoab:
22 socks: 12
and this is the definition of the element showing as two dimensional array...
<td><img alt="White Thoab" src="images/whitethoub.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="whitethoab" name="basket[whitethoab]" type="text" style="width:40px;"/> </td>
Something like:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="12" id="itemName" name="basket[chocolate]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="9" id="itemName" name="basket[onions]" type="text" style="width:40px;"/></td>
...and...
<?php
echo "<h1>The Items Are...</h1><br>";
foreach ($_POST['basket'] as $name => $value)
{
if($value > 0){
echo $name . ": " . $value . "<br>";
}
}
/* Output:
chocolate: 12
onions: 9
*/
?>
?
Not sure, but i think you want this
foreach ($itemsBasket as $key => $value)
{
if($value > 0){
echo $key. "<br>\n"
echo $value . "<br>\n";
}
}
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))