I have a form that has 8 columns and a variable number of rows which I need to email to the client in a nicely formatted email. The form submits the needed fields as a multidimensional array. Rough example is below:
<input name="order[0][topdiameter]" type="text" id="topdiameter0" value="1" size="5" />
<input name="order[0][bottomdiameter]" type="text" id="bottomdiameter0" value="1" size="5" />
<input name="order[0][slantheight]" type="text" id="slantheight0" value="1" size="5" />
<select name="order[0][fittertype]" id="fittertype0">
<option value="harp">Harp</option>
<option value="euro">Euro</option>
<option value="bulbclip">Regular</option>
</select>
<input name="order[0][washerdrop]" type="text" id="washerdrop0" value="1" size="5" />
<select name="order[0][fabrictype]" id="fabrictype">
<option value="linen">Linen</option>
<option value="pleated">Pleated</option>
</select>
<select name="order[0][colours]" id="colours0">
<option value="beige">Beige</option>
<option value="white">White</option>
<option value="eggshell">Eggshell</option>
<option value="parchment">Parchment</option>
</select>
<input name="order[0][quantity]" type="text" id="quantity0" value="1" size="5" />
This form is formatted in a table, and rows can be added to it dynamically. What I've been unable to do is get a properly formatted table out of the array.
This is what I'm using now (grabbed from the net).
<?php
if (isset($_POST["submit"])) {
$arr= $_POST['order']
echo '<table>';
foreach($arr as $arrs)
{
echo '<tr>';
foreach($arrs as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
echo '</table>;
};
?>
This works perfectly for a single row of data. If I try submitting 2 or more rows from the form then one of the columns disappears. I'd like the table to be formatted as:
| top | Bottom | Slant | Fitter | Washer | Fabric | Colours | Quantity |
------------------------------------------------------------------------
|value| value | value | value | value | value | value | value |
with additional rows as needed. But, I can't find any examples that will generate that type of table!
It seems like this should be something fairly straightforward, but I just can't locate an example that works the way I need it too.
How about this?
$keys = array_keys($_POST['order'][0]);
echo "<table><tr><th>".implode("</th><th>", $keys)."</th></tr>";
foreach ($_POST['order'] as $order) {
if (!is_array($order))
continue;
echo "<tr><td>".implode("</td><td>", $order )."</td></tr>";
}
echo "</table>
A Table class I wrote some time ago
<?php
class Table {
protected $opentable = "\n<table cellspacing=\"0\" cellpadding=\"0\">\n";
protected $closetable = "</table>\n";
protected $openrow = "\t<tr>\n";
protected $closerow = "\t</tr>\n";
function __construct($data) {
$this->string = $this->opentable;
foreach ($data as $row) {
$this->string .= $this->buildrow($row);
}
$this->string .= $this->closetable;
}
function addfield($field, $style = "null") {
if ($style == "null") {
$html = "\t\t<td>" . $field . "</td>\n";
} else {
$html = "\t\t<td class=\"" . $style . "\">" . $field . "</td>\n";
}
return $html;
}
function buildrow($row) {
$html .= $this->openrow;
foreach ($row as $field) {
$html .= $this->addfield($field);
}
$html .= $this->closerow;
return $html;
}
function draw() {
echo $this->string;
}
}
?>
To be used like this :
<body>
<?php
$multiDimArray = []; # Turn the form array into a matrix
for ($i = 0; $i < count($_POST['order']); $i++) {
$multiDimArray[] = [];
foreach ($_POST['order'][$i] as $key=>$value) {
if ($i == 0) {
$multiDimArray[$i][] = $key;
}
$multiDimArray[$i][] = $value;
}
}
$table = new Table($multiDimArray); # Create and draw the table
$table->draw();
?>
</body>
In your HTML, try something like this:
<table>
<tr>
<th>Bottom</th>
<th>Slant</th>
<th>Fitter</th>
</tr>
<?php foreach ($_POST['order'] as $order): ?>
<tr>
<td><?php echo $order['bottomdiameter'] ?></td>
<td><?php echo $order['slantheight'] ?></td>
<td><?php echo $order['fittertype'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Obviously, I'm not including all your attributes there, but hopefully you get the idea.
Related
Iam trying a solution in PHP/MYSQL where in there is a some country list with a checkbox. Certain countries have something called entities which is a drop down which shows Micro, Small, Large as a dropdown. This entity dropdown appears for certain countries only. I am able to pass the checkbox checked values to next page. But i need to pass the value of the entity selected in the drop down also. Iam not able to acheieve that. My code ie posted below. Here i need to pass the value of the drop down (select - entity_selected) also to next page:
<?php
echo '<table border="1">';
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1["country"];
$entity1 = $row1["entity"];
echo '<tr>';
?>
<td><input name="check_list[]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?></td>
<td>
<?php
if($entity1 == 'Yes'){ ?>
<select class="form-control selectpicker" name="entity_selected">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php }
?>
</td>
<?php echo '</tr>'; } ?>
</table>
The Next page code is below to get the check box selected countries.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
How can i get the drop down values here? iam getting the checkbox (countries values correctly). Can anyone pls help me on this pls?
<form method="POST" action="nextpage.php">
<table border="1">
<?php
$i = 0;
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1['country'];
$entity1 = $row1['entity'];
echo "<tr>";
?>
<td>
<input name="check_list<?=$i ?>" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
</td>
<td>
<?php
if($entity1=="Yes") {
?>
<select class="form-control selectpicker" name="entity_selected<?=$i ?>">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php
};
?>
</td>
<?php
$i++;
echo "</tr>";
};
?>
</table>
</form>
nextpage.php:
<?php
$entities = $checklist = [];
foreach($_POST as $k => $v) {
if(preg_match("/^checklist(\d+)$/", $k, $matches))
$checklist[intval($matches[0])] = $v;
unset($matches);
if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
$entities[intval($matches[0])] = $v;
};
?>
I am programming a voting system where users can upload up to 20 images per project (see attached image). Currently I am displaying per Row the project ID, an image and a text (text is the same in the same project). When the user adds 20 Images to the project the text will be shown 20 times. Now I am trying to rebuild the display part (see the attached Image) - instead of 20 rows, there should only be one row per user and project, but I have no clue how to achieve this. Thank you for your help.
This is my SQL syntax:
SELECT * from wp_awa_upload WHERE wp_awa_upload.uid = '$_SESSION[id]' and stat < 3 order by parent_cat, id asc
And this is the Part where I print the result of the table:
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
<? $i = 0; ?>
<? while ($show = mysql_fetch_array($abfrage)) { ?>
<form action="" method="POST">
<td><? echo $show['parent_cat'];?><br><input name="uid" type="#hidden" class="style13" value="<?php echo $show['uid']; ?>" id="uid" style="width: 40px" readonly /></td>
<td align="center"><? echo $show['file_name']; ?><br><div class="center-cropped"><img src="<? echo $show['url'] ?>" border="0"><br></div><br>©: <input name="copyright" type="text" class="style13" value="<?php if ($rolle <> '1') {echo $show['copyright'];} ?>" id="copyright" style="width: 140px" /><br><? if ( $show['youtube'] <> '') { echo 'Youtube'; }?></td>
<td><textarea name ='project' rows='16' cols='30' style="resize: none;"><?php if ($i == 0) { echo $show['project']; } ?></textarea></td>
<td><textarea name ='testimonial' rows='16' cols='30' style="resize: none;"><?php echo $show['testimonial']; ?></textarea><br>
<? if ($show['parent_cat'] == "Bester Styled Shoot") { ?>Styled Shoot Teilnehmer<br>
<textarea name ='styled' rows='8' cols='30' style="resize: none;"><?php echo $show['styled']; } ?></textarea></td>
<td><textarea name ='beschreibung' rows='16' cols='30' style="resize: none;"><?php echo $show['beschreibung']; ?></textarea></td>
<td><? if ($rolle <> 1 && $show['stat'] == 0 ) { echo '<button type="submit" value="' . $show['id']. '" name="change">Speichern?</button>'; ?><br><? echo '<button type="submit" value="' . $show['id']. '" name="delete">Löschen?</button>'; } ?></td>
<td><? if ($rolle == '1') { ?>
<select name="rating" class="style28" style="width: 120px" id="rating">
<option value="0">0</option>
<option value="5">5</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
<option value="60">60</option>
<option value="65">65</option>
<option value="70">70</option>
<option value="75">75</option>
<option value="80">80</option>
<option value="85">85</option>
<option value="90">90</option>
<option value="95">95</option>
<option value="100">100</option>
</select> <? echo '<button type="submit" value="' . $show['id']. '" name="submit">Bewerten?</button>'; }
elseif ($rolle == 9) {
echo '<button type="freigeben" value="' . $show['id']. '" name="freigeben">Freigeben?</button>';
echo '<button type="verwerfen" value="' . $show['id']. '" name="verwerfen">Verwerfen?</button><br><br>';
echo '<hr><br>'; ?>
E-Mail an Kunden schreiben:<br><textarea name ='informed' rows='8' cols='30' style="resize: none;"></textarea><br>
<? echo '<button type="informieren" value="' . $show['id']. '" name="informieren">Informieren?</button>';
}?></td>
</form>
I tried with an $i = 0; and then with a "if $i == 0 {} but, this is not working if a user has more than 1 project (the other project will not be displayed)
Thank you for your help!
Kind Regards, Stefan
I guess you want to display common text for all similar project images and id with It's related id & image.
You can do this by creating sub-array for managing image & id & main array for text description. Whenever same description received it'll break the loop & sub sequent data will be added as sub-array of main array. Try this. Hope it helps
$mainArry = array();
foreach($arrData as $item){
$idx = 0;
foreach($mainArry as $cat){
if($cat['text_description'] == $item['text_description']){
break;
}
$idx++;
}
$itemArry = array('id'=> ,'images' => );
if($idx >= 0){
$mainArry[$idx]['text_description'] = $item['text_description'];
$mainArry[$idx]['items'][] = $itemArry;
}
}
You should be doing a preprocessing of data (data massaging) before you use it for your template.
You can start by grouping rows with similar project
Example:
$processed_data = array();
foreach($rows as $row){
$processed_data[$row["project_id"][] = $row;
}
In this way, you can have something like this;
[
1 => [
[project 1, image1...]
[project 1, image2...]
[project 1, image3...]
],
2 => [
[project 2, image1...]
[project 2, image2...]
.....
]
]
Then in your template, it will be easier for you to display data with similar group
There are two methods I have mentioned below.
Please note that, these solutions are given based on following assumtions
1. You are using PHP
2. Database field names are userid, prjoject, text, image
3. You have stored imge src in the database.
Method 1:
SELECT userid, project,
GROUP_CONCAT(image separator ',') as images_list,
GROUP_CONCAT(image separator ',') as text_list
FROM your_table
GROUP BY userid,project
Above query returns data as you expected. Now you just need to populate those data into a html table.
Method 2:
Group data by user and project in your php code using a associative array.
$sql = "SELECT * FROM your_table";
Get query result to a PHP associative array. I assume the variable name is $data
$result = array();
foreach($data as $row) {
$userid = $row['userid'];
$project = $row['project'];
$result[$userid][$project]['text'][] = array('text' => $row['text'], 'image' => $row['image']);
}
HTML:
<table>
<?php foreach($result as $key_1 => $projects) { //userid holds $key_1 ?>
<?php foreach($projects as $key_2 => row ) {?>
<tr>
<td><?php echo $key_1; ?></td>
<td><?php echo $key_2; ?></td>
<td>
<table>
<tr><td><?php echo $row['text']?></td><tr>
<tr><td><img src="<?php echo $row['image']?>"></td><tr>
</table>
</td>
</tr>
<?php }?>
<?php } ?>
</table>
Following links may be helpful for more understanding.
MySql GROUP_CONCAT
PHP Associative array.
Hi thank you for your answers so far. I am nearly at the point where I could say "solved" :-)
$sql = "SELECT * from wp_awa_upload where stat = '0' order by parent_cat asc";
if (!$result = $mysqli->query($sql)) {
// Oh no! The query failed.
echo "Sorry, the website is experiencing problems.";
// Again, do not do this on a public site, but we'll show you how
// to get the error information
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
$last_entry = null;
echo "<table width='100%' border='1' align='center'><tr>";
echo "<tr>";
echo "<td align='center'>Projektnamen</td>";
echo "<td align='center'>UID</td>";
echo "<td align='center'>Kategorie</td>";
echo "<td align='center'>Projektbeschreibung</td>";
echo "<td align='center'>Beschreibung</td>";
echo "<td align='center'>Copyright</td>";
echo "<td align='center'>Tools</td>";
echo "</tr>";
while ($row = $result->fetch_object()) {
echo "<tr>";
if ($last_entry != $row->project) {
echo "<td align='center'><textarea name ='project' rows='16' cols='30' style='resize: none'>".$row->project."</textarea></td>";
echo "<td align='center'>".$row->uid."</td>";
echo "<td align='center'>".$row->parent_cat."</td>";
echo "<td align='center'><textarea name ='project_desc' rows='16' cols='30' style='resize: none'>".$row->project_desc."</textarea></td>";
echo "<td align='center'><textarea name ='beschreibung' rows='16' cols='30' style='resize: none'>".$row->beschreibung."</textarea></td>";
echo "<td align='center'>".$row->copyright."</td>";
echo "<td align='center'>Buttons</td>";
echo "</tr>";
echo "<tr>";
$last_entry = $row->project;
}
//echo "<td align='center' colspan='6'><img src=".$row->url."<border='0'> </td>";
echo "<td align='center' colspan='7'>";
echo "<a href='".$row->url."' data-lightbox='".$row->file_name."' data-title='".$row->file_name."'><br><div class='center-cropped'><img src='".$row->url."' border='0'></a></div>";
}
echo "</tr>";
echo "</table>";
The Description, Category,... will be displayed only once, the images will be displayed, but not side by side - they will be displayed with a line break.
My Question: How can I display the images side by side (4 in a row?) As I could have up to 20 Images in a project, it would be nice to show them in a pack of 4 and a max. of 5 Rows.
Thank you for you help and assistance.
Kind Regards,
Stefan
I am having trouble creating an addition table for my php program. I created my multiplication table just fine (it creates the right output), but when I try and make an addition one the numbers arent inside the table and I cant figure out what I am doing wrong. I will post what my output looks like when I create an addition table, and what it is supposed to look like and also the code that I have wrote. I feel like I am very close to completing this program, but right now I am stuck, thanks for the help in advance.
Here is the output that I am supposed to get for a 4x5 addition table.
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Here is my output .
11112222333344445555
0 1 2 3 4
1
2
3
4
5
And here is my code. Any help is appreciated.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++){
echo '<td>' .$c*$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else if (isset($_POST) && $_POST["operation"] == "addition")
{
echo 'This is a '. $_POST["rows"]. ' x ' . $_POST["columns"] .' addition table';
echo "<table border = 1";
echo'<tr>';
for($a = 0; $a <= $_POST["columns"];$a++){
echo '<th>'.$a.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo '<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++)
{
echo '<td>' .$c+$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>
try this.
Note: i only changed elseif statement which you used for additioin, so you can just copy and paste this as i changes nothing else.
so i divided the solution in three part and leave comment in bold for you to know which part i changed. thanks.
and post the comment what should you do next. In case it did not give you desired out come.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
// for($c = 1; $c <= $_POST["columns"]; $c++){
// echo '<td>' .$c*$r. '</td>';
//}
echo '</tr>';
}
echo "</table>";
}
From here i change the code In case it does not give you desired outcomes. you only have to change for loop part just change $_post['rows'] to $_post['columns'] in the first foreach loop. And in the second for loop chnage $_post['columns'] to $_post['rows']. Thanks
else if (isset($_POST) && $_POST["operation"] == "addition")
{
for($x=0; $x<$_POST['rows']+1; $x++)
{
$i=0;
$y= $i+$x;
echo '<table border 1px solid black>';
echo '<tr>';
//$value[]=$x;
echo '<td>'.$y.'</td>';
for($value=1;$value<$_POST['columns']+1;$value++){
$c=$y+$value;
// $c=$y+$value;
echo '<td>'.$c;
echo '</td>';
}
echo '</tr>';
echo '</table>';
}
My changes finished Every thing is your code as it is.
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>
It is actually a simple bug!
There is no error in your logic. The bug is in your echo statement for addition.
You just can't echo the addition directly. Use a bracket to surround them instead. It will work :-)
Like this;
echo '<td>'.($c+$r).'</td>';
Please let us know if it solved your problem.
Thanks,
Im a newbie to this forum and have just started coding in php. Need some help. I have the following code
<?php error_reporting(-1);
require_once('/mysql/sql_connect.php');
$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
$r = #mysqli_query ($dbc, $q);
$results_array = array();
while($row = mysqli_fetch_assoc($r)) {
array_push($results_array, $row);
echo "<pre>"; print_r($results_array); echo "</pre>"; }
?>
<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
<input type="submit" name="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.
If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.
I hope I've made sense. Thanks in advance.
It will obviously echo the key, as you assigned the value of options as $key
if you need the options in the $_POST['pty_select'] use this:
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $value['profile'];?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
You mean you need the value what you have used to display it.
Then,
Change to :
<option value="<?php echo $value['profile']; ?>">
<?php echo $value['profile']; ?>
</option>
And now let's go to ideal world :)
Build data pairs database_id => name for options:
$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users
WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);
$values = array();
while($r = mysqli_fetch_row($r)) {
$values[$r[0]] = $r[1];
}
Never use # when working with database, why do you want to suppress errors instead of preventing/handling them?
Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:
function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
// Header
echo '<select';
foreach( $attributes as $key => $val){
echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
}
echo '>';
// Values
foreach( $values as $key => $val){
echo '<option value="' . htmlspecialchars( $key) .'"';
if( $key === $selected_value){
echo ' selected="selected"';
}
echo '>' . htmlspecialchars( $val) . '</option>';
}
echo '</select>';
}
And now usage :)
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<?php selectbox( $values, array( 'name' => 'pty_select')); ?>
<input type="submit" name="Submit" />
</form>
And what to do with it then?
$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
$name = $values[$id];
}
Give
<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option>
instead of
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
Change it to something like
if(isset($_POST['Submit'])) {
echo $results_array[$_POST['pty_select']]['profile'];
}
Or alternatively use profile option value.
I am not sure if this is possible since I do not have much experience with foreach statements, but this is what I have
<?php $string = file_get_contents('data.xml') ?>
<?php
include("../connect.php");
$xml = new SimpleXMLElement($string);
//Loop trough multiple products
print "<table border='1' bordercolor='#6600FF' style='width='100%' cellpadding='3' cellspacing='3'>
<th>Campaign Name</th><th>Countries</th><th>Rate</th><th>Cash</th><th>Points</th>";
$cats = mysql_query("SELECT * FROM `offer_cats`");
while ($off = mysql_fetch_array($cats)) {
$cnames = array($off['name']);
$cnamess = implode(",", $cnames);
$cname = explode(",", $cnamess);
print_r($cnames);
foreach ($cnames as $category) {
$cat = $category;
}
}
foreach($xml->item as $item) {
$count = count(explode(", ",$item->countries));
if ($count >= 5) {
$country = "ALL INTL";
} else {
$country = $item->countries;
}
$rate = number_format((float)$item->rate, 2, '.', '');
$crates = $rate * 0;
$prates = $rate * 45;
$crate = number_format((float)$crates, 2, '.', '');
$prate = number_format((float)$prates, 2, '.', '');
echo'<tr><td>'.$item->name.'<br /><font color="limegreen" size="2">Incent: '.$item->incent.'</font><br /><select name="req" style="width:200px"><option value ="'.$item->requirements.'">'.$item->requirements.'</option></select>
<select style="width:200px" name="cat" id="cat"><option value="'.$cat.'">'.$cat.'</option></select><input type="button" name="add" value="+" /></td>';
echo '<td>'.$country.'</td>';
echo '<td>'.$item->rate.'</td>';
echo '<td><input type = "text" name="cash" value="'.$crate.'" style = "width:75px" /></td>';
echo '<td><input type = "text" name="points" value="'.$prate.'" style = "width:75px" /></td>';
// echo $item->incent;
// echo '<br/>';
}
?>
</tr>
</table>
I am trying to load all of the categories in this line <select style="width:200px" name="cat" id="cat"><option value="'.$cat.'">'.$cat.'</option></select> but it always loads only the last category in the array. Although I am pretty sure that the implode and explodes aren't really needed, it was an attempt to fix the error, but it was in vain. What exactly am I doing wrong?
Your first loop essentially just keeps overwriting the $cat variable:
foreach($cnames as $category){
$cat = $category;
}
You need to put this loop instead down where you want to loop through and output the options, like
echo '<select style="width:200px" name="cat" id="cat">';
foreach($cnames as $category){
echo '<option value="'.$cat.'">'.$cat.'</option>';
}
echo '</select>';