I'm a beginner in php and my instructor gave us a task. How can I get the value of the column in the selected row?
<?php
include 'connection.php';
$sqlsearch = "SELECT `Student_ID`,`First_Name`,`Last_Name`,`Year_Level`,`Enrollment_Date`,`Status` FROM `student_info`";
$sqlresult = $connection->query($sqlsearch);
$searchInput = "";
if($sqlresult->num_rows <= 0){
echo "No found Result";
}
if(!empty($_GET["search"])){
$searchInput = trim_input($_GET["search"]);
$sqlsearch = "SELECT `Student_ID`,`First_Name`,`Last_Name`,`Year_Level`,`Enrollment_Date`,`Status` FROM `student_info`
WHERE `Student_ID` = '". $searchInput ."' OR `First_Name` LIKE '%". $searchInput ."%' OR `Last_Name` LIKE '%". $searchInput."%'";
$sqlresult = $connection->query($sqlsearch);
if($sqlresult->num_rows > 0){
while($row = $sqlresult->fetch_assoc()){
generateResult($row);
}
}else{
}
}
else{
while($row = $sqlresult->fetch_assoc()){
generateResult($row);
}
}
function generateResult($row){
echo "<tr>";
echo '<td style="color:#33F0FF"> '. $row["Student_ID"] .'</td>'; //Plz get the student ID of the selected ID.
echo '<td>'. $row["First_Name"] .' '. $row["Last_Name"] .'</td>';
echo '<td>'. $row["Year_Level"].'</td>';
echo '<td>'. $row["Enrollment_Date"].'</td>';
if($row["Status"] == "Active"){
echo '<td style="color:green">'. $row["Status"].'</td>';
}else if($row["Status"] == "Dropped"){
echo '<td style="color:orange">'. $row["Status"].'</td>';
}else{
echo '<td style="color:red">'. $row["Status"].'</td>';
}
echo "</tr>";
}
?>
In the function generateResult($row)
how can I get the value of Student_ID when I click the link?
If you need student_id with anchor tag , probably you want to go to other page on clicking the link . In that case , replace :
echo '<td style="color:#33F0FF"> '. $row["Student_ID"] .'</td>';
to
echo '<td style="color:#33F0FF"> '. $row["Student_ID"] .'</td>';
Then on the xyz.php page , you will get student_id using $_GET["student_id"]
Related
PHP failed to print data of two columns of a mysql tableļ¼the result only shows the first row of data which proved to be right,with the rest rows show
" Undefined offset".
$query1='select MNO from mima01 order by MNO;';
$query2='select MTYPE from mima01 order by MNO;';
$result1=mysqli_query($conn,$query1);
$result2=mysqli_query($conn,$query2);
$username=mysqli_fetch_array($result1);
$usertype=mysqli_fetch_array($result2);
$count='select count(*) from mima01;';
$usercount=mysqli_query($conn,$count);
$usernum=mysqli_fetch_array($usercount);
for($i=0;$i<$usernum[0];$i++) {
echo '<tr><td style="text-align:center">' ;
echo $username[$i];
echo '</td>';
echo '<td style="text-align:center">' ;
echo $usertype[$i];
echo '</td></tr>';
}
I've rewritten your code to the normal way you would walk through a query result:
$query = 'select MNO, MTYPE from mima01 order by MNO;';
$result = mysqli_query($conn, $query);
while ($data = mysqli_fetch_array($result)) {
echo '<tr><td style="text-align:center">' .
$data['MNO'] .
'</td>' .
'<td style="text-align:center">' .
$data['MTYPE'] .
'</td></tr>';
}
Please note that I have no way to run this code so I can't be a 100% sure it will work.
from what I understand I think your solution is:
$query='SELECT MNO,MTYPE FROM mima01 ORDER BY MNO DECS;'; //use DECS|ASC
$result=mysqli_query($conn,$query);
$username=mysqli_fetch_array($result);
// to count the rows
$count=mysqli_num_rows($result);;
echo $count;
// to display data
while($row=mysqli_fetch_array($result) {
echo '<tr><td style="text-align:center">';
echo $row['MNO'];
echo '</td>';
echo '<td style="text-align:center">';
echo $row['MTYPE'];
echo '</td></tr>';
}
Hope it was helpful
I'm trying to make a webshop for an assignment and I can't figure out how to display that there are 2 of the same product in my checkout. This is my code I want to display something like: Article1 .. 2x Article2 ... 5x etc.
<?php
session_start();
//read out session with article numbers
$array = $_SESSION['mandje'];
echo '<center>';
echo '<h1> Uw mandje: </h1>';
echo '<table style="border: 2px solid black">';
//array that checks if there are two of the same article numbers
$mandje = array();
//read out array
foreach ($array as $artikel)
{
echo '</br>';
echo $artikel;
if (in_array($artikel, $mandje)){
//I need to display the article that i have multiple times here i guess
} else {
//getting the articles out of the database
require ('config.php');
$query = "SELECT * FROM mphp6_meubels WHERE artikelnr = $artikel";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '</tr>';
}
}
//adding the article to array so i can check if there are multiple of the same articles in array
$mandje[] = $artikel;
}
echo '</table>';
echo '</center>';
Just group by artikelnr field.
foreach ($array as $artikel)
{
require ('config.php');
$query = "SELECT mphp6_meubels.*, count(*) as cnt FROM mphp6_meubels WHERE artikelnr = $artikel GROUP BY artikelnr";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td>' . $artikel . '</td>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '<td> ' . $meubel['cnt'] . '</td>';
echo '</tr>';
}
}
when select data from database using php-mysql and display in html table it shows as (A).
but i want to display it as (b)
help me...
Sample (and very simple) code would be:
// assuming the PDO connection is established and kept in $db
$st = $db->prepare("SELECT Date, Num, Value1, Value2 FROM the_table");
if ($st->execute()) {
$previousDate = NULL;
$previousNum = NULL;
while ($row = $st->fetch()) {
echo '<tr>';
echo '<td>';
if ($previousDate != $row['Date']) {
echo $row['Date'];
$previousDate = $row['Date'];
}
echo '</td>';
echo '<td>';
if ($previousNum != $row['Num']) {
echo $row['Num'];
$previousNum = $row['Num'];
}
echo '</td>';
echo '<td>'. $row['Value1'] .'</td>';
echo '<td>'. $row['Value2'] .'</td>';
echo '</tr>';
}
}
I have created an sql database(with phpmyadmin) filled with measurements from which I want to call data between two dates( the user selects the DATE by entering in the HTML forms the "FROM" and "TO" date) and display them in a table.
Additionally I have put, under my html forms, some checkboxes and by checking them you can restrict the amount of data displayed.
Each checkbox represent a column of my database; so along with the date and hour column, anything that is checked is displayed(if none is checked then everything is displayed).
So far I managed to write a php script that connects to the database, display everything when none of my checkboxes is checked and also managed to put in order one of my checkboxes.
Problem: The data that I call for are been displayed twice.
Question: I want to have four checkboxes.
Do I need to write an sql query for every possible combination or there is an easier way?
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_Database_Test = "localhost";
$database_Database_Test = "database_test";
$table_name = "solar_irradiance";
$username_Database_Test = "root";
$password_Database_Test = "";
$Database_Test = mysql_pconnect($hostname_Database_Test, $username_Database_Test, $password_Database_Test) or trigger_error(mysql_error(),E_USER_ERROR);
//HTML forms -> variables
$fromdate = $_POST['fyear'];
$todate = $_POST['toyear'];
//DNI CHECKBOX + ALL
$dna="SELECT DATE, Local_Time_Decimal, DNI FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\"";
$tmp ="SELECT * FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\"";
$entry=$_POST['dni'];
if (empty($entry))
{
$result = mysql_query($tmp);
echo
"<table border='1' style='width:300px'>
<tr>
<th>DATE</th>
<th>Local_Time_Decimal</th>
<th>Solar_time_decimal</th>
<th>GHI</th>
<th>DiffuseHI</th>
<th>zenith_angle</th>
<th>DNI</th>
";
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['Local_Time_Decimal'] . "</td>";
echo "<td>" . $row['Solar_Time_Decimal'] . "</td>";
echo "<td>" . $row['GHI'] . "</td>";
echo "<td>" . $row['DiffuseHI'] . "</td>";
echo "<td>" . $row['Zenith_Angle'] . "</td>";
echo "<td>" . $row['DNI'] . "</td>";
echo "</tr>";
}
echo '</table>';}
else
{
$result= mysql_query($dna);
echo
"<table border='1' style='width:300px'>
<tr>
<th>DATE</th>
<th>Local_Time_Decimal</th>
<th>DNI</th>
";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['Local_Time_Decimal']."</td>";
echo "<td>" . $row['DNI'] . "</td>";
echo "</tr>";
}
echo '</table>';
}
if($result){
echo "Successful";
}
else{
echo "Enter correct dates";
}
?>
<?php
mysql_close();
?>
Try to create your checkbox like below:
Solar_Time_Decimal<checkbox name='columns[]' value='1'>
GHI<checkbox name='columns[]' value='2'>
DiffuseHI<checkbox name='columns[]' value='3'>
Zenith_Angle<checkbox name='columns[]' value='4'>
DNI<checkbox name='columns[]' value='5'>
And try to hange your PHP code to this:
<?php
//HTML forms -> variables
$fromdate = isset($_POST['fyear']) ? $_POST['fyear'] : data("d/m/Y");
$todate = isset($_POST['toyear']) ? $_POST['toyear'] : data("d/m/Y");
$all = false;
$column_names = array('1' => 'Solar_Time_Decimal', '2'=>'GHI', '3'=>'DiffuseHI', '4'=>'Zenith_Angle','5'=>'DNI');
$column_entries = isset($_POST['columns']) ? $_POST['columns'] : array();
$sql_columns = array();
foreach($column_entries as $i) {
if(array_key_exists($i, $column_names)) {
$sql_columns[] = $column_names[$i];
}
}
if (empty($sql_columns)) {
$all = true;
$sql_columns[] = "*";
} else {
$sql_columns[] = "DATE,Local_Time_Decimal";
}
//DNI CHECKBOX + ALL
$tmp ="SELECT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\"";
$result = mysql_query($tmp);
echo "<table border='1' style='width:300px'>
<tr>
<th>DATE</th>
<th>Local_Time_Decimal</th>";
foreach($column_names as $k => $v) {
if($all || (is_array($column_entries) && in_array($k, $column_entries)))
echo "<th>$v</th>";
}
echo "</tr>";
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['Local_Time_Decimal'] . "</td>";
foreach($column_names as $k => $v) {
if($all || (is_array($column_entries) && in_array($k, $column_entries))) {
echo "<th>".$row[$v]."</th>";
}
}
echo "</tr>";
}
echo '</table>';
if($result){
echo "Successful";
}
else{
echo "Enter correct dates";
}
?>
<?php
mysql_close();?>
This solution consider your particular table columns but if your wish a generic solution you can try to use this SQL too:
$sql_names = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$database_Database_Test' AND TABLE_NAME = '$table_name'";
and use the result to construct the $column_names array.
Solution for your problem : Change the mysql_fetch_assoc with mysql_fetch_array
If you have the same problem try to print your result with print_r
Answer : Use the bit datatype in mysql for store and read your checkboxes.
When you're receiving thr value from the database then you can use
in the parameter checked you can use the php code as exist :
$value = ...get the value from db ( 1 or 0 )
echo '<input type="checkbox" name="thename" value="thevalue" '.($value==1?'checked=checked'.'').'/>';
I'm creating a small private forum to get some more knowledge about PHP/PDO etc. Now I have a weird bug/error/wrong piece of code that is not showing the echo. This is my code.
$sql2 = $db->prepare('SELECT topic_id, topic_subject,topic_date,topic_cat FROM topics WHERE topic_cat = :topid');
$sql2->bindParam(':topid', $_GET['id'], PDO::PARAM_INT);
$sql2->execute();
$result2 = $sql->rowCount();
if($result2 === FALSE){
echo 'The topics could not be displayed, please try again later.';
}
elseif ($result2 === 0){
echo 'There are no topics in this category yet.';
} else {
//prepare the table
echo '<table border="1">
<tr>
<th>Topic</th>
<th>Created at</th>
</tr>';
while($row = $sql2->fetch()) {
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3>' . $row['topic_subject'] . '<br /><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
}
}
It should show the echo at while($row = $sql2->fetch()), but it is not. Also I know there is not enough { and } but that's because the other part of the code is not relevant.
You appear to count the rows returned by $sql then loop through $sql2. Have you checked to see if there are any results in $sql2?