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>';
}
}
Related
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>
I am getting the name of the database table heads and data like this:
/* Get column names */
public function get_head($table)
{
$this->table = $table;
$query = $this->conn->prepare("DESCRIBE ".$this->table);
$query->execute();
return $query->fetchAll(PDO::FETCH_COLUMN);
}
/* List all users */
public function get_users($table)
{
$this->table = $table;
$query = $this->conn->prepare("SELECT * FROM ".$this->table);
$query->execute();
return $query->fetchAll();
}
and they are output like this:
<table border=1>
<?php
// Get headers
$columns = $user->get_head('login');
foreach ($columns as $col)
{
echo '<th>'.$col.'</th>';
}
// Get users
$users = $user->get_users('login');
foreach ($users as $user)
{
echo '<tr>';
echo '<td>'.$user['user_id'].'. </td>';
echo '<td>'.$user['user_name'].'</td>';
echo '<td>'.$user['email'].'</td>';
echo '<td>'.$user['password'].'</td>';
echo '<td>'.$user['role'].'</td>';
echo '<td>'.$user['last_login_date'].'</td>';
echo '<td>'.$user['date_created'].'</td>';
echo '<td>'.$user['status'].'</td>';
echo '</tr>';
}
?>
How can I write the second foreach to generate the data automatically based on the names output by the first foreach of the table headers?
In your code where you want to show the data for each row, you can foreach() over the $columns and use the column name as the index to your user data...
echo '<tr>';
foreach ( $columns as $column ) {
echo '<td>'.$user[$column].'</td>';
}
echo '</tr>';
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);
I am querying my database for results (which all display correctly) and then while running a foreach statement I am attempting to look in another table for a matching ID to gather an override price which will take the place of the 'storeprice' listed in the first result.
Here is my model:
public function fetch_products($limit, $start, $manuid) {
$this->db->order_by('productname', 'ASC');
$this->db->limit($limit, $start);
$query = $this->db->get_where('products', array('manuid' => $manuid, 'active' => 1));
if($query->num_rows() > 0){
foreach($query->result() as $row){
$data[] = $row;
$pid = $row->id;
// Check for Price Override
$squery = $this->db->get_where('price_override', array('id' => $pid));
if($squery->num_rows() > 0){
$result = $squery->row();
$override = $result->storeprice;
$data['override'] = $override;
} else {
$override = 0;
$data['override'] = $override;
}
}
return $data;
}
return false;
}
The $data['override'] is what is causing me an error. The following error to be precise:
Message: Undefined property: stdClass::$override
In the controller I am using the following:
$data['results'] = $this->store_products_model->fetch_products($config["per_page"], $page, $manuid);
And finally in the view I am calling the results inside of a table to display. All will display except for the override:
foreach ($results as $product){
echo '<tr>';
echo '<td>' . $product->productname . '</td>';
if($product->override != 0){
echo '<td>$' . $product->override . '</td>';
} else {
echo '<td>$' . $product->storeprice . '</td>';
}
echo '<td>$' . $product->saleprice . '</td>';
echo '<td>' . $product->storepoints . '</td>';
echo '<td>Edit';
echo '</tr>';
}
Anyone see what I could be doing wrong here to give me the following error?
Message: Undefined property: stdClass::$override
The row is an object, not an array. Try:
$data->override = $override
edit:
Well, actually, $data is an array, but your inserting an object into it, so it would be
$last_index = sizeof($data) - 1;
$data[$last_index]->override = $override;
I'm making this function where a part of an array should be read, and each value holds a number with which I want to perform a PDO query. This is my following code:
function get_topics($array) {
$top = 20; $base = 0;
foreach ($array as $key => $value) {
$getData = $dbc->prepare('SELECT * FROM topics WHERE id = :id LIMIT 1');
$getData->execute(array(':id' => $value));
while($row = $getData->fetch()) {
$potential_topic_img = 'members/topic_' . $value . '.jpg';
if (file_exists($potential_topic_img)) { $topic_img = $potential_topic_img; } else {
$topic_img = 'members/0.jpg'; }
$name = $row['name'];
echo '<div class="topic_div"><img src="' . $topic_img . '" width="80"><br /><span
style="font-size:10pt;">' . $name . '</span></div>';
} if (++$base == $top) break;
}
}
echo get_topics($some_array);
But all I get is an error telling this: "Parse error: syntax error, unexpected T_VARIABLE in /home/......", and it says that the problem is on this line:
$getData->execute(array(':id' => $value));
What can I be doing wrong?
EDIT
I deleted some code and the code is running fine when this is remaining:
function get_topics($array) {
foreach ($array as $key => $value) {
echo $value;
}
}
echo get_topics($user_likes_array);
So it's not that $value is empty, the problem seems to be in the line I mentioned in the beginning, since when I move everything below that line, the error message does not change, but it does change when I move that specific line.
foreach ($array as $key => $value) assigns the current element's key to the $value variable on each iteration.
Try
$getData->execute(':id',$value);
The code you have posted is correct are you sure your in the right file.
I have just coped your code and sorted out the layout and ran it though my PHP Testing batch and it is correct
Output from testing batch
G:\Others\Programs\phpApplications>SET PHP_PATH=../php/php.exe
G:\Others\Programs\phpApplications>SET FILE_PATH=../phpApplications/test2.php
G:\Others\Programs\phpApplications>SET LOOP=FALSE
G:\Others\Programs\phpApplications>"../php/php.exe" "../phpApplications/test2.ph
p"
G:\Others\Programs\phpApplications>pause
Press any key to continue . . .
Code i used
<?php
function get_topics($array) {
$top = 20;
$base = 0;
foreach ($array as $key => $value) {
$getData = $dbc->prepare('SELECT * FROM topics WHERE id = :id LIMIT 1');
$getData->execute(array(':id' => $value));
while($row = $getData->fetch()) {
$potential_topic_img = 'members/topic_' . $value . '.jpg';
if (file_exists($potential_topic_img)) { $topic_img = $potential_topic_img; } else {
$topic_img = 'members/0.jpg'; }
$name = $row['name'];
echo '<div class="topic_div"><img src="' . $topic_img . '" width="80"><br /><span
style="font-size:10pt;">' . $name . '</span></div>';
}
if (++$base == $top) break;
}
}
$some_array = array();
echo get_topics($some_array);
if there was an error you would have seen it before the pause command is sent to CMD it would have shown the error
Proof My Tester Works
for this i just commented out // the $some_array = array() here is the result
G:\Others\Programs\phpApplications>SET PHP_PATH=../php/php.exe
G:\Others\Programs\phpApplications>SET FILE_PATH=../phpApplications/test2.php
G:\Others\Programs\phpApplications>SET LOOP=FALSE
G:\Others\Programs\phpApplications>"../php/php.exe" "../phpApplications/test2.ph
p"
Warning: Invalid argument supplied for foreach() in G:\Others\Programs\phpApplic
ations\test2.php on line 6
G:\Others\Programs\phpApplications>pause
Press any key to continue . . .