Hey guys i was just playing around with datatables, i was wondering if anyone new of a possible way to do the following:
When you have a TD for Description, if the description is lengthy is there a way to add like a read more link or a ... instead of warping the row height?
Any resources you have on this would be useful!
Here is my current setup:
require_once('db_config.php');
$sql = "SELECT * FROM routes";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo '<table class="table table-striped table-bordered" id="basic-datatable" border="0" cellpadding="10">';
echo '<thead><tr><th>Business ID</th><th>Business Type</th><th>Price</th><th>Down Payment</th><th>Weekly Net</th><th>City</th><th>State</th><th>Business Description</th></tr></thead>';
echo '<tbody>';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo '<td>' . $row['route_id'] . '</td>';
echo '<td>' . $row['route_title'] . '</td>';
echo '<td>$' . $row['total_price'] . '</td>';
echo '<td>$' . $row['down_payment'] . '</td>';
echo '<td>$' . $row['weekly_net'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['state'] . '</td>';
echo '<td>' . $row['remarks'] . '</td>';
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
Thanks
There are two ways to go about this kind of behavior.
One is CSS using text-overflow: ellipsis;, though that only handles the ellipsis part. (Since you want more than just that, I won't go into the details here, but here are the MDN docs for that, in case you're interested.)
The way to do it in PHP is to determine how many characters fit into your table cell, including "Read More," and truncating the string accordingly.
So, if $row['remarks'] is the field you want to handle, you might do something like:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if(strlen($row['remarks']) >= 50) {
$row['remarks'] = substr($row['remarks'],50) . "... Read More";
}
echo '<tr>'...
}
Related
In the beginning, each person have 2 languages of messages (chimessage
& engmessage)saved to the database.
Each of them have selected a preferred language (either chi or eng).
In the display page, i'm trying to show each person's message with their preferred language.
eg, If person A prefer eng, the page will only show engmessage.
PS the display page will include both engmessage and chimessage for different people
I am wondering how can i do that, im thinking using the if/else statement.
PS I know iam using deprecated mysql,still trying to learn php, plz bear with me
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM table
WHERE language ='Chi' OR language ='Eng'");
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="200px">' . $row['people'] . '</td>';
echo '<td width="200px">' . $row['language'] . '</td>';
echo '<td>' . $row['chimessage'] ."<br />";
echo '<td>' . $row['engmessage'] ."<br />";
}
echo "</tr>";
echo "</table>";
?>
Expected result
People Language Msg
A Chi 物理治療
B Eng Physiotherapy
Change your code like this
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM table
WHERE language ='Chi' OR language ='Eng'");
echo "<table>";
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="100px">' . $row['id'] . '</td>';
echo '<td width="200px">' . $row['people'] . '</td>';
if($row['language'] == 'Chi')
{
echo '<td>' . $row['chimessage'] ."</td>";
}
else if($row['language'] == 'Eng')
{
echo '<td>' . $row['engmessage'] ."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Firstly i'm not a developer so bear with me :)
I'm trying to delete a row from a small inventory system we are setting up but its still not allowing me to do anything, this is my current code:
Assigned page:
<?php
$password = 'notTheRealPassword';
$conn = mysql_connect('localhost', 'root', $password);
if(!$conn)
{
die('Error connecting database');
}
mysql_select_db('inventory_system', $conn);
$query= "SELECT * from assigned";
$result=mysql_query($query);
echo "<table border='0' cellpadding='100' class='table-striped'>";
echo "<th>Asset ID</th><th>Asset</th><th>Type</th><th>Manufacturer</th><th>Model</th><th>PC
Name</th><th>Serial Number</th><th>Purchased</th><th>Warranty
End</th><th>OS</th><th>OS
Bit</th><th>CPU</th><th>Memory</th><th>HDD</th><$
while($row = mysql_fetch_array($result))
{
echo '<td>' . $row['asset_id'] . '</td>';
echo '<td>' . $row['asset'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td>' . $row['manufacturer'] . '</td>';
echo '<td>' . $row['model'] . '</td>';
echo '<td>' . $row['pc_name'] . '</td>';
echo '<td>' . $row['serial_no'] . '</td>';
echo '<td>' . $row['purchased'] . '</td>';
echo '<td>' . $row['warranty_end'] . '</td>';
echo '<td>' . $row['os'] . '</td>';
echo '<td>' . $row['os_bit'] . '</td>';
echo '<td>' . $row['cpu'] . '</td>';
echo '<td>' . $row['memory'] . '</td>';
echo '<td>' . $row['hdd'] . '</td>';
echo '<td>' . $row['plant'] . '</td>';
echo '<td>' . $row['location'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
Delete page:
<?php
$password = 'notTheRealPassword';
$conn = mysql_connect('localhost', 'root', $password);
if(!$conn)
{
die('Error connecting database');
}
mysql_select_db('inventory_system', $conn);
$id = (int)$_GET['asset_id'];
mysqli_query($conn,"DELETE FROM assigned WHERE asset_id='".$id."'");
mysqli_close($conn);
header("Location: assigned1.php");
?>
I'm guessing it will be something simple.
Cheers,
There are a few things wrong here:
Your GET variable, the one you put in the link, is id, not asset_id
You should not use GET to modify things in your database; if your browser or a plugin decides to pre-fetch links, your database will be wiped out.
You can't mix mysql apis, use either mysqli or mysql (although not the latter...).
The mysql_* functions are deprecated, you should use PDO or mysqli in combination with prepared statements.
When you echo out lots of html, it is easier to just close the php section and then open it again when you need it; now you have php tags inside the echoed texts. The php / strings / html are not separated correctly.
I'm having trouble on While Loop. Here is my code:
<?php
//DISPLAY'S SKILL LIST
$showItemList = $con->query('SELECT * FROM items');
if ($showItemList->num_rows > 0) {
$x=1;
// output data of each row
while($row = $showItemList->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $x++ . '</td>';
echo '<td>' . $row['item_id'] . '</td>';
echo '<td>' . $row['item_name'] . '</td>';
echo '<td>' . $row['item_category'] . '</td>';
echo '<td>' . $row['item_costprice'] . '</td>';
echo '<td>' . $row['item_retailprice'] . '</td>';
echo '<td>' . $row['item_tax'] . '%</td>';
echo '<td>';
$showInHouseQty = $con->query('SELECT SUM(item_quantity) AS TotalQuantityInStock FROM item_inventory_inhouse WHERE item_id="'.$row['item_id'].'"');
while($row = $showInHouseQty->fetch_assoc()) {
echo $row['TotalQuantityInStock'];
}
echo '</td>';
echo '<td>';
$showPharmaQty = $con->query('SELECT SUM(item_quantity) AS TotalPharmaQty FROM item_inventory_inpharmacy WHERE item_id="'.$row['item_id'].'"');
while($row = $showPharmaQty->fetch_assoc()) {
echo $row['TotalPharmaQty'];
}
echo '</td>';
echo '</tr>';
}
}
?>
Well, the problem is, I can't get the result of $row['TotalPharmaQty'];
If you use the same variables in nested while loops as are in the parent while loop, they will overwrite. Change $row in the second and third while loops to $row2 and $row3 respectively, and it should work fine.
I am trying to store my outputted table from mySQL query into session variables and for use in another page. Here's what I have so far, but I do not get an output:
First page that displays the results of my search:
<?php
session_start();
$personid = ($_POST['personid']) ? $_POST['personid'] : $_GET['personid'];
if (empty($personid)) {
echo 'Please enter some search parameters';
} else {
$sql = "SELECT * FROM persons WHERE 1=1";
if ($personid)
$sql .= " AND personid='" . mysqli_real_escape_string($mysqli,$personid) . "'";
$total_records = mysqli_num_rows(mysqli_query($mysqli,$sql));
$sql .= " ORDER BY surname";
$loop = mysqli_query($mysqli,$sql)
or die ('cannot run the query because: ' . mysqli_error($mysqli));
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Name</th> <th>Address</th> <th>City</th> <th>Province</th> <th>Postal Code</th> <th>Phone Number</th> <th>Email</th> <th></th></tr>";
while ($record = mysqli_fetch_assoc($loop)) {
echo "<tr>";
/* echo '<td>' . $record['firstname'] . $record['surname'] .'</td>';*/
echo "<td>$record[firstname] $record[surname]</td>";
echo '<td>' . $record['address'] . '</td>';
echo '<td>' . $record['city'] . '</td>';
echo '<td>' . $record['province'] . '</td>';
echo '<td>' . $record['postalcode'] . '</td>';
echo '<td>' . $record['phone'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo ("<td>Edit</td>");
echo "</tr>";
}
echo "</table>";
$_SESSION['animals']=$loop;
echo "<center>" . number_format($total_records) . " search results found</center>";
}
?>
Click here to see if sessions work
This then goes to this script:
<?php
session_start();
echo "The results";
while ($record = mysqli_fetch_assoc($_SESSION['animals'])) {
echo "<tr>";
echo "<td>$record[firstname] $record[surname]</td>";
echo '<td>' . $record['address'] . '</td>';
echo '<td>' . $record['city'] . '</td>';
echo '<td>' . $record['province'] . '</td>';
echo '<td>' . $record['postalcode'] . '</td>';
echo '<td>' . $record['phone'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo ("<td>Edit</td>");
echo "</tr>";
}
What might the problem be? Thanks in advance.
Session are meant to store primitive data types.
Storing a resulstset into a session is a terrible idea and mostly likely bad for server ressources.
If you really need that data somewhere else in your app just query your database again which is not overly expensive.
I can see the problem that you are trying to store the mysqli resource object to the session.
$loop = mysqli_query($mysqli,$sql)
If you want to store the final html to the session, then create a single variable that holds final html string.
$html = "<table>";
while ($record = mysqli_fetch_assoc($loop)) {
$html .= $record['something'];
// Likewise add other variables to html string.
}
$html . = "</table>";
$_SESSION['animals'] = $html
But if you want the array that holds all the database result then you can do like this
$rows = array();
while ($record = mysqli_fetch_assoc($loop)) {
$rows[] = $record;
}
$_SESSION['animals'] = $rows;
Now you use this session value in another pages and iterate and create html.
Just as other website with session variables, using isset will make the script run smoothly
if (!isset($_SESSION['basket'])){
$_SESSION['basket'] = array();
}
Pascal
multiskillz
I'm currently trying to pull out records from database and display it on my php form for users to update it. In my database table, there's a column named stage. Under stage, there are foundation, intermediate and advance. Here's an example of it:
Valid XHTML http://img259.imageshack.us/img259/2461/databasei.png.
Valid XHTML http://imageshack.us/photo/my-images/849/profileint.png/.
So when i press the foundation button in my php form(2nd picture), only records where theirs stage are foundation, will be shown on the list(together with their name, contact number, email and student ID)
Is there any way that i can do that
Are you looking for something like this?
<?php
$stage = $_GET['stage'];
$sql = 'SELECT * FROM {$tablename} WHERE stage=:stage';
$db = new PDO('mysql:host=localhost;dbname=yourdbname', $user, $pass);
try {
$statement = $db->prepare($sql);
$statement->execute(array(':stage', db->quote($stage)));
foreach($statement->fetchAll() as $row) {
// Do things here.
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
<?php
include('connect-db2.php');
// get results from database
$result = mysql_query("SELECT * FROM players2 WHERE stage = 'foundation'")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>S/N</th> <th>Student_ID</th> <th>Name</th> <th>Contacts No.</th> <th>Email</th><th>Stage</th></tr>";
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['student_id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['contact_no'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['stage'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
?>