Pass selected option value to mysql query - php

I have an html select that is filled with data from the database.
It looks like this
echo '<select name="client_list">';
foreach ($htmlselect as $key => $row) {
echo
'<option value='.$row->CUSTOMERCODE.'>'
.$row->CUSTOMERNAME. '</option>';
}
echo '</select>';
Below it I have a table that is populated with values from the DB.
$table = $wpdb->get_results('SELECT STRING, DATE, STRING02 FROM `CUSTOMERFILE` WHERE `code` = 2 AND `status` = 2');
echo '<thead ><tr >';
foreach ($header as $list) {
echo '<th >' . $list . '</th>';
}
echo "<thead></tr>";
if ( !empty( $table) ) {
foreach ( $table as $key => $value ) {
echo '<tr >';
foreach ( $value as $a ) {
if (empty($a)) {
# code...
echo '<td>NULL</td>';
} else {
echo '<td>' . $a . '</td>';
}
}
echo '</tr>';
}
}
Now in this query I want to add to the where another condition that takes the selected option value from the html select
It would look something like this
$table = $wpdb->get_results('SELECT STRING, DATE, STRING02 FROM `CUSTOMERFILE` WHERE `code` = 2 AND `status` = 2 **AND STRING05 = selected option value**');
How do I pass the selected value in the query?

The $wpdb->get_results() function take 2 parameters, the first is the query and the second is the output
$wpdb->get_results($command, $output);

Related

PHP code to separate Table based on column values

I have a csv file that should be converted to HTML table based on column values , and I have to put the values into separate table based on first column of csv . And I think the problem is because of '\n' in first column.
So its like this:
Here in Result column, in one row I have three values separated using comma (W,M,P). In the code I wrote it is considered as separate table headers .
Can anyone please help me with this?
This is my code:
<?php
$csv="FinalResult.csv" ;
$csvcontents=file_get_contents($csv);
$csv_array = explode("\n", $csvcontents);
$tables = [];
foreach($csv_array as $key => $value) {
if ($key == 0) {
continue;
}
$line = explode(',', $value);
if (array_key_exists($line[0], $tables)) {
$tables[$line[0]][] = $line;
} else {
$tables[$line[0]] = [$line];
}
}
foreach ($tables as $key => $value) {
echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
echo "<table>";
echo '<tr>';
foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
if (in_array($keyHeader, [0, 1])) {
continue;
}
echo "<th>$valueHeader</th>";
}
echo '</tr>';
foreach ($value as $keyRow => $valueRow) {
echo '<tr>';
foreach ($valueRow as $keyValue => $valueValue) {
if (in_array($keyValue, [0, 1])) {
continue;
}
echo "<td>$valueValue</td>";
}
echo '</tr>';
}
echo '</table>';
}
?>
I refereed in stack overflow , but there they were giving only single values to a column and not providing multiple values .
But am getting output like this ,
so it takes the value of Result column after '-' as a new heading , i tried but am not able to solve this , can anyone really help me in this matter .
Here is how my output should look like:
I marked in yellow where all the data am getting in same column
This is my csv file :
Team,Date,Opponent,Result
MIN,May-03,UTA,a.b.c=d-e.f-g.h=log4j2-i.xml-j -k -a4j.k=tp_r-RR.xml -
MIN,May-04,SEA,"L,Q,J"
SAC,May-03,DAL,L
SAC,May-04,TOR,W
NYN,May-05,BAL,L
NYN,May-07,MIA,W
Here is the code
<table>
<?php
$csvValues= array_map('str_getcsv', file('FinalResult.csv'));
$counter = 0;
// Header
echo "<tr>";
foreach($csvValues[0] as $headers){
echo "<th>".$headers."</th>";
}
echo "</tr>";
// Content
foreach($csvValues as $values){
echo "<tr>";
if($counter >0){
foreach($values as $data){
echo "<td>".$data."</td>";
}
}
echo "</tr>";
$counter++;
}
?>
</table>

How to show json data with column name in php

i have some JSON data
i am able to retrieve data out of it but i want data to be displayed with columns
this the JSON
{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]}
here is what i am doing right now
$jsonData = json_decode($data, TRUE);
foreach ($jsonData as $d) {
foreach ($d as $ins) {
echo "<h3>".$ins['ObjectName']."</h3>";
echo "<h3>".$ins['ObjectDescription']."</h3>";
}
}
OutPut:
MEGA MELA
NEW EVENT BY API
Event x11
NEW EVENT BY API
i want it in this format
ObjectId | ObjectName | ObjectTitle | ObjectDescription | ObjectLabel | ObjectTypeId | MaxFieldsExpected | ObjectValueType | ObjectControlType | IsDeleted | CreatedDate | CreatedBy | EditedDate | EditedBy
43 | MEGA MELA | Event Created by API | NEW EVENT BY API | ..............................................................................
above i am getting data with help of array key now i want extracting keys from JSON like ObjectId or ObjectName making them header of table and showing all the data in columns
on the above example, you can use the key to print the filed name and value will be the data.
here we can loop the field names first to create the heading
I am using table here.
<table>
<?php foreach ($d as $ins) { ?>
//printing field heads
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$key."</h3></td>";
} ?>
</tr>
//printing value row
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$value."</h3></td>";
} ?>
</tr>
} ?>
</table>
Used an old gist for array2table from here : http://www.aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
<?php
function array2table($array, $recursive = false, $null = ' ')
{
// Sanity check
if (empty($array) || !is_array($array)) {
return false;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
}
// Start the table
$table = "<table>\n";
// The header
$table .= "\t<tr>";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . $heading . '</th>';
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
$table .= "\t<tr>" ;
foreach ($row as $cell) {
$table .= '<td>';
// Cast objects
if (is_object($cell)) { $cell = (array) $cell; }
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . array2table($cell, true, true) . "\n";
} else {
$table .= (strlen($cell) > 0) ?
htmlspecialchars((string) $cell) :
$null;
}
$table .= '</td>';
}
$table .= "</tr>\n";
}
$table .= '</table>';
return $table;
}
$data ='[{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]';
$jsonData = json_decode($data, TRUE);
echo array2table($jsonData);
?>
DEMO: https://3v4l.org/0n27Y
Second foreach should look like: foreach ($d as $key => $ins) then at $key variable you get your indexes.

foreach inside foreach error [duplicate]

This question already has answers here:
show name from inner join with the id [duplicate]
(2 answers)
Closed 5 years ago.
here my code in class.php:
public function select(){
$stmt = $this->conn->prepare("SELECT country FROM `country`") or die($this->conn->error);
if($stmt->execute()){
$result = $stmt->get_result();
return $result;
}
}
public function read(){
$stmt = $this->conn->prepare("SELECT segment FROM `segments`") or die($this->conn->error);
if($stmt->execute()){
$result = $stmt->get_result();
return $result;
}
}
and now in index.php i did this:
<th class="text-center">country</th>
<th class="text-center">segments</th>
</thead>
<tbody>
<?php
require 'class.php';
$conn = new db_class();
$read = $conn->select();
$test = $conn->read();
while($fetch = $read->fetch_array(MYSQLI_ASSOC)&& $fetch1 = $test->fetch_array(MYSQLI_ASSOC)){
foreach ($fetch1 as $field => $values) {
foreach($fetch as $field=>$value){
echo '<tr><td>' . $value . '</td>'
. '<td>'. $values .'</td>';
}
}
}
?>
</tbody>
</table>
im just trying to fetch data from 2 tables and put them in one html table
i get this error for 6 times:
Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\segments\countrySegment.php on line 59
im just trying to fetch data from 2 tables and put them in one html table
and if any one know how, i want for every country a drop down menu with segment values inside
any idea? thank u in advance
This because of array pointer issue
foreach ($fetch1 as $field => $values) {
foreach($fetch as $field=>$value){
echo '<tr><td>' . $value . '</td>'
. '<td>'. $values .'</td>';
}
}
first time second foreach prints everything next time it has no value it points beyond the value..
so you need to reset the array using reset
foreach ($fetch1 as $field => $values) {
reset($fetch)
foreach($fetch as $field=>$value){
echo '<tr><td>' . $value . '</td>'
. '<td>'. $values .'</td>';
}
}
or you can save it a temporary variable
foreach ($fetch1 as $field => $values) {
$fetchtmp = $fetch;
foreach($fetchtmp as $field=>$value){
echo '<tr><td>' . $value . '</td>'
. '<td>'. $values .'</td>';
}
}

PHP - Update HTML Select list to show current value

I'm working on a simple HTML form that will include a drop down list for the selected Salesperson. The list is coming from a database and is in an array that is stored in a variable $ids and looks like this:
Array ( [John Jones] => JJ [Sally Smith] => SS [Victor Howards] => VH [Barnie Kemp] => BK )
Here's how the select list is currently constructed:
<?php
$output = "";
$selected = false;
foreach ($ids as $id => $value) {
$output .= "<option value=\"$value\"";
if ($id == $record->getField('initials')) {
$selected = true;
$output .= " selected";
$selectedID = $record->getField("id");
}
$output .= ">$id</option>";
}
if (!$selected) {
$chosen = $record->getField("initials");
$output = "<option value=\"$chosen\" selected>$chosen</option>$output";
}
echo $output;
?>
I'm trying to update the select list to show the current value for this field as the selected option. I have the name/value stored in these variables:
$initials
$name
So if $initials = 'SS' I would like Sally Smith to be the selected value in the list.
Try to use as simple as follow
<?php
$output = "";
foreach ($ids as $id => $value) {
$selected = "";
if ($id == $record->getField('initials')) {
$selected = "selected";
}
$output .= "<option value='$value' $selected>$id</option>";
}
echo $output;
?>
Do not make your code complex.. do it like this one:
$value=$record->getField('initials');
foreach ($ids as $id => $valueData) {
if($id==$value)
echo "<option value='".$id."' selected='selected' >$id</option>";
else
echo "<option value='".$id."'>$id</option>";
}

How to count the mutli-dimensional arrays' value in PHP

I tried to create a table that calculating the data which stored in mutli-dimensional arrays' value and show the result by table format.
I don't know how to count the values which stored in the array's array(third level array's value.
Can I using the function to count values in the mutli-dimensional arrays' value? Hope anyone can teach me, thanks.
<?php
$itemList = array
("book"=>array("ID"=>"14567",
"name"=>array("History"=>array("American"=>12,"Europe"=>2), "SF"=>array("Space"=>32), "Chinese"=>array("kungFu"=>10))),
"stationary"=>array("ID"=>"24547", "name"=>array("HB"=>array("ABC"=>123, "MC"=>161,"GCD"=>26)))
);
$item = "<table border=1>";
$item .= "<td>item count(s)</td>";
foreach($itemList as $key => $value){
$item .= "<tr>";
$item .= "<td align=center>" .
/*count the categories of the items(e.g. American,Europe,Space,Kungfu show
the result: "4")*/
. "</td>";
$item .= "</tr>";
}
$item .= "</table>";
echo $item;
?>
foreach($itemList['book']['name'] as $key => $value)
{
foreach ($itemList['book']['name'][$key] as $key1 => $value1) {
$category[] = $key1;
$value2[] = $value1;
}
}
echo count($category);
here you got the total numebr of count of your category

Categories