PHP form is not displaying data from MySQL - php

PHP form is not displaying data from MySQL. I have a simple 4 text area form that I would like the data in the specific table rows to display in their respective area.
The first display area does display the data from table and specific row
ex: ( I have attempted to use the endwhile: but I get an error
<?php
//Create Select Query
$query = "SELECT * FROM worksheet";
$data = mysqli_query($con, $query);
?>
Area 1 - Does display data in area 1 of form
<?php while($row1 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row1['u_i'] ;
echo "\r";
}
?>
Area 2 does not display data from MySQL
<?php while($row2 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row2['u_ni'] ;
echo "\r";
}
?>
Area3 does not display data from MySQL
<?php while($row3 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row3['nu_i'];
echo "\r";
}
?>
Area4 does not display data from MySQL
<?php while($row4 = mysqli_fetch_assoc($data)) {
echo "\r";
echo $row4['nu_i'];
echo "\r";
}
?>
Someone please show me the way. Thanks

1st you have to push result into array.Then you can display the anywhere as you wish.
$query = "SELECT * FROM FROM worksheet";
$data_arr=array();
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
//push elements into array
array_push($data_arr,$obj->field_name)
}
/* free result set */
$result->close();
}
Display the result.
echo "\r";
echo $data_arr[0]['nu_i'];
echo "\r";
..
..
..
echo "\r";
echo $data_arr[3]['nu_i'];
echo "\r";

Related

Variables inside while loop

I have following code, I try to style output with CSS, but I have small problem, my code show all database entries, which is OK, but when I remove comment from WHILE loop, and comment echo, its showing only first row of entries from database.how can I do same thing and show multiple results from database by use variables in While Loop?:
<?php
error_reporting(0);
require 'connect.php';
$search = $_POST['search'];
//$checkout = $_POST['checkout'];
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM area where destination='{$search}'";
$result = mysqli_query($conn, $sql);
if($count = $result->num_rows) {
echo '<p>', $count, '</p><br><br>';
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row['destination'],' ',$row['place'], $row['tosee'], '<br>';
/$destination =$row["destination"];
//$place =$row["place"];
/$destination =$row["destination"];
//$place =$row["place"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
and inside my HTML file:
location: <?php echo $destination; ?>
places: <?php echo $place ; ?>
views: <?php echo $tosee; ?>
Do this...
$out .= $row['destination'].' '.$row['place'].' '.$row['tosee']. '<br>';
Then you can use the $out variable anywhere else...
Even for these...
$destination .= $row["destination"];
$place .= $row["place"];
Since well....are in the while loop
I found a few small errors, you had a ',' instead of a '.' to concatenate a variable.
if($count = $result->num_rows) {
echo '<p>' . $count . '</p><br><br>';
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row['destination'].' '.$row['place'].' '.$row['tosee']. '<br>';
//$destination =$row["destination"];
//$place =$row["place"];
//$destination =$row["destination"];
//$place =$row["place"];
}
}
and for php comments it is: //

Php query based on selected/default dropdown and output on same page

I have managed to create dropdown from Mysql column and also get query result with get method but here webpage is directed to other page when hit button.
I am looking to get query output with some default option set in dropdown when page loads or want query result on same page reloading it again when user changes option.
Any help will be appreciated.
Code on main page for dropdown:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id");
echo "<form action='process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name=nx_version>";
while ($row = $result->fetch_assoc()) {
unset($nx_version);
$nx_version = $row['nx_version'];
echo '<option value>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'>See items</button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
echo "<form>";
Code I wrote when hit button and gives query output (in process.php):
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()) {
$array[] = $result1['step1'];
}
print_r($array);
process.php file should be like this -
<?php
session_start();
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()){
$array[] = $result1['step1'];
}
$_SESSION['data'] = $array;
// storing the data as session
header("location:main_page.php");
?>
Now get back the data from session in your main page by adding this-
$array = $_SESSION['data'];

PHP selecting from database

I'm having trouble selecting data from inside one of the tables in the database.
I'm trying to display a user's profile when their name is clicked on.
<?php
$view = mysql_fetch_array(mysql_query("select * from users where id=$view"));
if (empty ($view[id])) {
print "No such user.";
exit;
}
print "<center><b><u>$view[user]</b></u> ($view[id])</center><br>";
print "Rank: $view[rank]<br>";
?>
The link they will click is:
viewpage.php?page_id=4&view=....
I can't get this to work.
<?php
$id=$_GET["view"];
$query = mysql_query("SELECT * FROM users WHERE id=$id");
if($query)
{
echo '<table>';
echo '<tr><th>Name</th><th>ID</th><th>Something Else</th></tr>';
while($view = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$view['Name'].'</td>';
echo '<td>'.$view['id'].'</td>';
echo '<td>'.$view['some other variable in your data'].'</td>';
echo '</tr>'; //This one goes after you've echoed all the data for one user.
}
echo '</table>';
} else {
echo 'no user found';
}
?>

match dropdown from retrieved result from mysql

I have an error here, the data retrived from mysql database and listed in listbox or dropdown, when i need to update my form i get the result after refresh.
I have tried but need to sortout
<?
include("connect.php");
mysql_select_db("joblisting") or die(mysql_error());
$result = mysql_query("SELECT * FROM positiontitle") or die(mysql_error());
$ans=$positiontitle;
echo $ans;
?><select size=1 name="positiontitle" id="positiontitle"><?
echo '<option value="'.$ans.'">';
echo "</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option>";
echo $row['positiontitle'];
echo "</option>";
}
echo "<br />";
?>
where are you defining $positiontitle ? shouldn't it be $_POST['positiontitle']?

Place php code inside floating css container(s)?

i have a question in regards to php and css layers.
i have the following php code:
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
//while ($db_field = mysql_fetch_assoc($result)) {
//print $db_field['planet_name'] . "<BR>";
//print $db_field['location'] . "<BR>";
while($row = mysql_fetch_array( $result )){
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo "<br> ";
}
which correctly displays a word [Planet name] and a number [System] in sequence.
The above code displays the information in rows such as;
Planet Sun is located in system 35.
Planet Saturn is located in system 30.
i'm just trying to make this information display a look a little nicer. in a way so planet name shows up in the right of a background image container and system in another corner possibly colored.
....
How do i place the above code inside floating css container(s)?
Thank you.
As it seems to be a list of planets, I would use an HTML list. You can edit the css to have it look the way you want after.
echo ' <ul class="planetList"> ';
while($row = mysql_fetch_array( $result )){
echo '<li>';
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo '</li> ';
}
echo '</ul>';
In CSS
ul.planetList {
display:block;
float:right;
background-image:url('yourBackground.jpg');
background-position:left;
background-repeat:repeat-y;
/* CSS 3 Only */
background-size:{length of your text}px 100%;
}
You could also use a table instead of the list. This way, you could get a background for the planet column, and another one for the located at sytem one.
echo ' <table> ';
echo ' <tr><th>Planet</th><th>System</th></tr>';
while($row = mysql_fetch_array( $result )){
echo '<tr>';
echo '<td class="planet">' . $row['planet_name'] . '</td>';
echo '<td class="system">' . $row['location'] . '</td>';
echo '</tr> ';
}
echo '</table>';
CSS :
table>tr.planet {
background-image:url('yourBackground.jpg');
background-position:left;
}
table>tr.system {
background-color:#CCFF00;
}
Php code should be inside tags. You can always close these tags and put some htmls tags
Example:
<div class="bla"><?php
my code
?></div>
Obviusly you can do everything in php
<?php
echo '<div class="bla">';
....phpcode....
echo '</div>';
?>
Hopefully this helps
You can do something like this to keep your code clean
<?php
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
$planets = array();
while($row = mysql_fetch_array( $result )){
$planets[] = $row;
}
foreach($planets as $planet): ?>
<div class="prettyFloatyDiv">
Planet, <?php echo $planet['planet_name']?>
is located at System <?php echo $planet['location']?>
<br/>
</div>
<?php endforeach; ?>
If you have short tags enabled, you can replace every <?php echo with <?=

Categories