How to generate links to images using php in a table - php

I am trying to create a table that will display the new suggested products by users to allow a moderator to to choose which get approved. For now, all I am trying to do is show the image, as well as the user that submitted it and some other info. I know my problem is with the bottom echo statement that tries to put together a working url by combining the file location and the name of the file itself from the database, which is stored like "Roots.jpg" or like "brock.jpg"
Thanks for any help. I am pretty new to php.
<html>
<title>Forum Approval</title>
<body>
<?php
$db_host = "host";
$db_username = "user";
$db_pass = "pass";
$db_name = "name";
$pdo = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT * FROM add_new_product";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<table border='1' align='center'>
<caption>Products to approve</caption>
<tr>
<th>Product Id</th>
<th>User Id</th>
<th>Name</th>
<th>Company</th>
<th>Image</th>
</tr>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>' . $row['new_prod_ID'] . '</td>';
echo '<td>' . $row['user_ID'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['company'] . '</td>';
echo "<td><img src = "a link/uploads/ . $row['image']" . width='100'></td>";
echo '</tr>';
}
?>
</table>
</body>
</html>

Try changing the echo to something like...
echo "<td><img src=\"path/to/directory/".$row['image']."\" width='100'></td>
The src would be path/to/directory/Roots.jpg

Related

PHP To Show 2 Tables On One Page

I am using this syntax to display a PHP Table on my page. I now need to add in a second table directly above this one, but all the syntax I try throws a 500 error. How can I with 1 connection to MSSQL run 2 Select statements and populate 2 individual html tables?
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
EDIT
This is the syntax I am trying to adapt, but I keep getting a 500 Error
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>YTD Pay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . "$" . round($res->PayYTD) . "</td>";
print "</tr>";
}
}
<br><br><br>
//Query
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
The problem you're having is that you are using the calls incorrectly.
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
The first line will create an object, it doesn't matter which. The object will be in $query.
The second line will immediately destroy the object and assign a string to $query (this is incorrect).
The third line expects an object as a parameter to setQuery, but unfortunately it is a string! Error.
If you want this to work correctly, then you need to use the object in $query correctly.
I'm not a Joomla expert, so I link you to a page for how to do this correctly: https://docs.joomla.org/Selecting_data_using_JDatabase
Put the output in a variable.
$StringOut = '';
$StringOut .= '<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>';
foreach ($query as $res)
{
$StringOut .= "<tr>";
$StringOut .= "<td>" . $res->name . "</td>";
$StringOut .= "<td>" . $res->hiredate . "</td>";
$StringOut .= "<td>" . $res->bday . "</td>";
$StringOut .= "<td>" . $res->payrate . "</td>";
$StringOut .= "<td>" . $res->hourlypay . "</td>";
$StringOut .= "</tr>";
}
$StringOut .= '</table>';
#Do other logic to retrieve first table.
#You could put it in a different variable if you like. And print when and whereever you wish.
echo $StringOut;
#Optionally close connection, done.

converting php code to html

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X.
Here's my Php code
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and here's what i have done so far for HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php mysql_close($connector); ?>
</body>
</html>
little Help here please, Thanks !!
EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.
you not close while;
so change code to :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
/////////////////////////////////// change \\\
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db($database, $connector)
or die("Unable to connect");
/////////////////////////////////// end change \\\
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) :
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php endwhile;?>
<?php mysql_close($connector); ?>
</body>
</html>
Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.
Error code :
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname);
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
//here there is no variable like what you passing to connect mysql that's problem here
?>
solution :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.
You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.
<?php
$host = 'localhost';
$userName = 'dbuser';
$password = 'pw';
$nameOfDb = 'dbname';
$nameOfTable = 'tablea';
$pdo = new PDO('mysql:host='.$host, $userName, $password);
$pdo->query("use " . $nameOfDb);
// desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
// - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
//$query = $pdo->prepare('select * from '.$nameOfTable);
$query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
</tr>
</thead>
<tbody><?php
forEach($rows as $curRow)
{
echo '<tr>';
// use this one if you want to display the columns in the same order they are returned in the query results
// AND you'd like to display all columns in the result
forEach($curRow as $curCell)
echo '<td>'.$curCell.'</td>';
// otherwise, you can request the desired elements by name
//
// echo '<td>' . $curCell['id'] . '</td>'
// echo '<td>' . $curCell['Subject_Code'] . '</td>'
echo '</tr>';
}
?></tbody>
</table>
</body>
Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

PHP there's only 1 row is returned then duplicates it

I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
It's only showing the 1st row in all 5 rows, because I placed it in a while loop (HTMLcode) so that it can print all the other rows not just the first one.
Image that shows the problem
THANK YOU IN ADVANCE :)
PHP CODE
$id = session_id();
if ($id == "") {
session_start();
}
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
// making the connection to the database
try {
$host = '127.0.0.1';
$dbname = 'webdev_2014376';
$user = 'root';
$pass = '';
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");
// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);
// taking each individual piece
$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
?>
HTML CODE
<?php
echo 'hello, ' . $_SESSION['username'];
echo ' ';
echo $_SESSION['id'];
?>
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="TableCol1"> Username </th>
<th class="TableCol2"> First Name </th>
<th class="TableCol3"> Last Name </th>
<th class="TableColOpt"> Options </th>
</tr>
</thead>
<tbody>
<?php
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $username . '</td> ';
echo '<td class="prEach1">' . $firstname . '</td> ';
echo '<td class="prEach3">' . $lastname . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
}
?>
</tbody>
</table>
</div>
-------------------------------------------------------
EDIT
I FOUND THE SOLUTION
SOLUTION IMAGE
PHP CODE
<?php
$id = session_id();
if ($id == "") {
session_start();
}
if (!isset($_SESSION['username'])) {
header("Location: login.php");
}
// making the connection to the database
try {
$host = '127.0.0.1';
$dbname = 'webdev_2014376';
$user = 'root';
$pass = '';
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
} catch(PDOException $e) {echo 'Error';}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// selecting the row from the database
$sqlQuery = $DBH->prepare("SELECT * FROM users");
// running the SQL
$sqlQuery->execute();
// pulling the data into a variable
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
?>
HTML CODE
<?php
echo 'hello, ' . $_SESSION['username'];
echo ' ';
echo $_SESSION['id'];
?>
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="TableCol1"> Username </th>
<th class="TableCol2"> First Name </th>
<th class="TableCol3"> Last Name </th>
<th class="TableColOpt"> Options </th>
</tr>
</thead>
<!-- This is the category fields on the list. -->
<tbody>
<?php
//$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $check['Username'] . '</td> ';
echo '<td class="prEach1">' . $check['FirstName'] . '</td> ';
echo '<td class="prEach3">' . $check['LastName'] . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC); //THIS SHOULD BE AT THE BOTTOM JUST BEFORE THE WHILE LOOP ENDS
}
?>
</tbody>
<!-- This is the get methods of the properties, where the output of the user put in the Property form will be shown -->
</table>
</div>
I was wondering if anyone can explain to me why its only returning 1 row when there are 5 different rows in mysql table.
Look what's happening inside while loop,
while ($check) {
echo '<tr>';
echo '<td class="prEach1">' . $username . '</td> ';
echo '<td class="prEach1">' . $firstname . '</td> ';
echo '<td class="prEach3">' . $lastname . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
$check = $sqlQuery->fetch(PDO::FETCH_ASSOC);
}
Initially you fetched only one row from the result set using $row = $sqlQuery->fetch(PDO::FETCH_ASSOC); and put the values in the respective variables. In the while loop you're looping through the result set but actually echoing same old variables.
Solution:
If you want to display all the result set rows(including the first row) inside your table cells, then first delete these four lines,
$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);
$UserID = $row['UserID'];
$username = $row['Username'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
And then loop through the result set like this,
// your code
<?php
while ($row = $sqlQuery->fetch(PDO::FETCH_ASSOC)){
echo '<tr>';
echo '<td class="prEach1">' . $row['Username'] . '</td> ';
echo '<td class="prEach1">' . $row['FirstName'] . '</td> ';
echo '<td class="prEach3">' . $row['LastName'] . '</td> ';
echo '<td class="prEach4 optlinks"> '
. 'View '
. '</td>';
echo '</tr>';
}
?>
// your code

Creating HTML table with php output

I am trying to get the php output into an HTML table. The mysql code outputs the data from the table pet, but when i add the table code as shown below it stops working and gives errors. How do I get the table pet output into an HTML table? thanks
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
echo "<table border = '2'>"
<tr>
<th>id</th>
<th>name</th>
</tr>
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>;
}
echo "</table>";
?>
</body>
</html>
Your issues lies in your formatting and confusion between the use of the echo command and non PHP wrapped HTML. Code should read as follows when properly formatted.
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<table border ="2">
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Your quoting is wrong.
echo "<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>";
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
The quote at the end of the first echo line belongs after the </tr>. And the last echo "</tr>"; line was missing a closing quote.
You should be able to see these problems from the syntax highlighting in the question.

php linebreak causes string value to disappear

I have some php querying and printing the info of a mysql table on a medical recruitment website.
Sometimes the users enter, causing line breaks where I would rather not have them, because
it affects the layout of the rendered HTML table in a way that it shouldn't.
I can trim the strings when they fill in the forms and the data get written into the table, but there's already a lot of data there, so i decided to remove the line breaks on the page
where php reads and prints the results.
i declared one of the strings where this causes trouble as such:
$specialtr = trim($special, "\n");
and then where the html table gets rendered:
echo '<td width="150">' . $specialtr . '</td>';
Here is the whole code:
All the code works, it's only the parts above that I added that now causes the column with $specialtr variable not to print/print as empty.
<form action="selected.php" method="POST">
<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">
<tr>
<td>
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error($conn));
mysql_select_db($dbname) or die(mysql_error($conn));
$specialtr = trim($special, "\n");
echo '<table border="1" cellpadding="2" cellspacing="2" style="margin-left: auto; margin-right: auto;">';
$query = 'SELECT userid FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$num_entries = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<tr>';
echo '<td><input type="radio" name="employee" value="' . $row['userid'] . '"/></td>';
echo '<td>Select</td>';
}
}
echo '</tr>';
echo '</table>';
?>
</td>
<td>
<?php
echo '<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">';
echo '<tr>';
$query = 'SELECT userid, name, surname, sex, age, nationality, email, telnr, special FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<td>' . $userid . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $surname . '</td>';
echo '<td>' . $sex . '</td>';
echo '<td>' . $age . '</td>';
echo '<td>' . $nationality . '</td>';
echo '<td>' . $email . '</td>';
echo '<td>' . $telnr . '</td>';
echo '<td width="150">' . $specialtr . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</td>
</tr>
</table>
<br/>
<br/>
<input type="submit" name="go" value="Go!" />
</form>
The code worked well enough, but since I added the trim, that entire column (the $special column disappears).
I hope maybe someone can tell me why it disappears or if my syntax has a little mistake.
Using extract() like that is a horrible thing. The function should be avoided as a general rule.
Also note that you're doing your trim() call BEFORE $special is defined, so your $specialtr variable is going to be an empty string. Calling the function before you start fetching your database results and extracting a result row into $special will not magically apply trim() to reach row you've fetched. If you want the results to be trimmed as you fetch them, then you have to apply the trim AFTER you do the fetch:
while ($row = mysql_fetch_assoc($result)) {
echo '<td>' . $row['userid'] . '</td>';
...
echo '<td width="150">' . trim($row['special']) . '</td>';
^^^^^^^^^^^^^^^^^^^^^
}

Categories