I am trying to print the contents of items by their category in a web page in this manner:
Category1:
Item1 item2 item3
item4 item5...
Category2
item1 item2 ...
This my PHP code:
$cat="";
$maxcols = 3;
$i = 0;
while ($row = $result->fetch_assoc()) {
if ($i == $maxcols) {
$i = 0;
echo "</tr><tr>";
}
if($row['name']!=$cat)
{
echo "<table>
<tr><td collspan='3'>".$row['name']."</td></tr>
<tr>";
}
echo "<td>".$row['title']."</td>";
$cat=$row['name'];
$i++;
}
while ($i <= $maxcols) {
echo "<td> </td>";
$i++;
echo "</table>";
}
What I am getting is:
<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr><tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td><table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td></table>
What I want to get is:
<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr>
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td>**<td> </td></tr>
</table>**
<table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td><td> </td>
</table>
First of all, you have very 'messy' code. I suppose you need some more code to add the part between the stars, but I think you should use a cleaner approach.
I suppose you are using mysql, which supports the GROUP_CONCAT()-function. It would be wise to use that in this case.
Make a query:
SELECT name, GROUP_CONCAT( title SEPERATOR '|') as titles FROM your_database GROUP BY name
The results per category are now in one row.
//Amount of rows
$maxcols = 3;
while( $row = $result->fetch_assoc() ) {
//The seperator needs to be something that isn't in the values it seperates
//This creates an array with the titles for this category
$titles = explode( '|', $row['titles'] );
echo '
<table>
<tr>
<td colspan="' .$maxcols. '">' .$row['name']. '</td>
</tr>';
//$offset + $i is the position in the array of titles
$offset = 0;
//This loop ensures each row is properly opened and closed
while( $offset < count( $titles ) ) {
echo '<tr>';
//This will ensure each row has $maxcols td's in it
for( $i = 0; $i < $maxcols; $i++ ) {
if( isset( $titles[ $offset + $i ] ) ) {
echo '<td>' .$titles[ $offset + $i ]. '</td>';
} else {
echo '<td> </td>';
}
}
$offset += $maxcols;
echo '</tr>';
}
echo '</table>';
}
Thank you very much, Sumurai8! Your idea works great. Here is my php code:
<?php
include_once('include/db.inc.php');
$sql="SELECT m.name, GROUP_CONCAT(s.title order by s.title SEPARATOR '|' ) as titles FROM menu m, software s where m.id=s.category
GROUP BY m.name
order by m.name";
$result = $mysqli->query($sql);
$cat="";
$maxcols = 3;
//$i = 0;
echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>content</title>
<meta content='text/html; charset=utf-8' http-equiv='content-type'>
<meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>
<link href='styles/style.css' rel='stylesheet' type='text/css'>
</head>
<body class='body'>";
while( $row = $result->fetch_assoc() ) {
//The seperator needs to be something that isn't in the values it seperates
//This creates an array with the titles for this category
$titles = explode( '|', $row['titles'] );
echo "
<table>
<tr><td colspan='" .$maxcols. "'>" .$row['name']. "</td></tr> \n";
//$offset + $i is the position in the array of titles
$offset = 0;
//This loop ensures each row is properly opened and closed
while( $offset < count( $titles ) ) {
echo "<tr>";
//This will ensure each row has $maxcols td's in it
for( $i = 0; $i < $maxcols; $i++ ) {
if( isset( $titles[ $offset + $i ] ) ) {
echo "<td>" .$titles[ $offset + $i ]. "</td>";
} else {
echo "<td> </td>";
}
}
$offset += $maxcols;
echo "</tr> \n";
}
echo "</table><br> \n";
}
This is the output, just the way I want it:
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>content</title>
<meta content='text/html; charset=utf-8' http-equiv='content-type'>
<meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>
<link href='styles/style.css' rel='stylesheet' type='text/css'>
</head>
<body class='body'>
<table>
<tr><td colspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr>
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdf</td><td> </td></tr>
</table><br>
<table>
<tr><td colspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td> </td><td> </td></tr>
</table><br>
</body>
</html>
Related
I wanted to create a multiplication table with a custom function and also get the number of rows and columns from the user, and I want if each row was an even number then its field would be red (background) and if it was odd number it would have a green background and i also write some codes but i'm not sure if it's true or not:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form method="post">
<input type="number" name="rows" placeholder="Rows">
<input type="number" name="columns" placeholder="Columns">
<button type="submit" name="button">
Create
</button>
</form>
<table border="1px">
<?php
if (isset($_POST["button"])) {
$userRows = $_POST["rows"];
$userColumns = $_POST["columns"];
function multiplicationTable($rows, $columns)
{
$zarb = $rows * $columns;
return $zarb;
}
$x = 1;
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
if ($x % 2 == 0) {
echo "<td style='background-color: red;'>" . multiplicationTable($x, $y) . "</td>";
} else {
echo "<td style='background-color: green;'>" . multiplicationTable($x, $y) . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
}
?>
</table>
</body>
</html>
How about changing the while loops to this:
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
$val = multiplicationTable($x, $y);
if ($val % 2 == 0) {
echo "<td style='background-color: red;'>" . $val . "</td>";
} else {
echo "<td style='background-color: green;'>" . $val . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
Well, your code looks good. Click here to see how it looks like...
the final result is in this link, and even if some of the numbers are even, they're still displayed in green and i don't know what should i do
https://imgur.com/D1Sweee
What i currently have
what im trying to make
I need it when i throw for example 2 and 3 it goes down 2 and marks it from left to right using black borders, and then it goes 3 to the right and mark from top to bottem using black borders, at the point they cross i need it to be a red border.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
echo "<table class='table'>";
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
echo "<td> <img class='icon cell color".mt_rand(1,4)."' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>```
You can check if dice1 value is equal to $i you can add a certain class that makes the color black. Same if dice2 value is equal to $i. If then dice1 is equal to $i and dice2 value is equal to $j choose a different class to make the border red.
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=0; $i < 6 ; $i++) {
echo "<tr>";
for ($j=0; $j < 6 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="form.css">
</head>
<body>
<div class="wrapper">
<?php
if(isset($_POST['submit'])) {
$dice1 = mt_rand(1,6);
$dice2 = mt_rand(1,6);
function chooseBorder($i,$j,$dice1,$dice2) {
$class = "";
if ($dice1 == $i && $dice2 == $j) {
$class = "border-red";
return $class;
}
if ($dice1 == $i || $dice2 == $j) {
$class = "border-black";
return $class;
}
return $class;
}
for ($i=1; $i < 7; $i++) {
echo "<tr>";
for ($j=1; $j < 7 ; $j++) {
$class = chooseBorder($i,$j,$dice1,$dice2);
echo "<td> <img class='icon cell color".mt_rand(1,4). " " . $class . "' src='img/icon".mt_rand(1,4).".svg ' </td>";
}
echo "</tr>";
}
echo "</table>";
echo "<img class='dice1' src='img/dice".$dice1.".gif'>";
echo "<img class='dice2' src='img/dice".$dice2.".gif'>";
}
?>
<form method="get" action="/php1/eindopdracht/cal.php">
<button type="submit" class="button1">renew</button>
</form>
<form method="POST">
<button type='submit' class='button2' name='submit' value='submit'>throw</button>
</form>
</div>
</body>
</html>
I'm trying to add a counter to the table in the following code. but I couldn't be successful. Can I get a little help, please? thanks. Something like this:
$counter = 0;
$counter++;
if($counter % 33 == 0)
So that when the counter is added, table will continue after %33 on the right of the page, and it will continue like that, instead of going down the page.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<table class="tbresult">
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
echo "</table>";
}
}
mysqli_close($link);
?>
</body>
</html>
First, don't have an HTML form inside a table, this is not valid, AFIK, and can cause you many troubles in different browsers.
You need simply open the table once, then create a counter = 0 and on each while loop add 1 to it. Then check, if it divides by 33 than you close the table and open a new one. After the loop you close the last table.
The side by side alignment can be done with CSS, something like .tbresult {float: left; width: 200px;}
Something like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
$table_row_counter = 0;
echo '<table class="tbresult">';
while($row = $result->fetch_assoc())
{
$table_row_counter++;
if ($table_row_counter % 33 == 0) {
echo '</table>';
echo '<table class="tbresult">';
}
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
}
}
mysqli_close($link);
?>
</body>
</html>
I am creating a table of checkboxes, where the first column will contain checkboxes that can check / uncheck the current row.
This is my code so far, which is not working.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
function toggle(source, $id) {
checkboxes = document.getElementsByName($id);
for (var i = 0, n = checkboxes.length; i < n; i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "
<tr>";
echo "
<td>";
echo '<input type="checkbox" onClick="toggle(this, $id)"/> Toggle All';
echo '
</td>
';
for ($y = 0; $y < 7; $y++){
echo "
<td>";
echo '<input type="checkbox" name=$id value="bar1"> Bar 1<br/>';
echo '
</td>
';
}
echo "
</tr>
";
}
echo "</table>";
?>
</body>
</html>
I am aware that the code might contain several errors and am trying to find them. In the meantime, i would appreciate any suggestions.
try this code
<!DOCTYPE html>
<html>
<head>
// your java script code for toggle is working only change is used id not $id.
<script language="JavaScript">
function toggle(source, id) {
checkboxes = document.getElementsByName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<?php
echo "<table>";
for ($x = 0; $x < 3; $x++) {
$id = $x;
echo "<tr>";
echo "<td>";
echo '<input type="checkbox" onClick="toggle(this, '.$id.')" /> Toggle All';
// change here onClick="toggle(this, $id)" to onClick="toggle(this, '.$id.')"
echo '</td>';
for ($y = 0; $y < 7; $y++){
echo "<td>";
echo '<input type="checkbox" name="'.$id.'" value="bar1"> Bar 1<br/>';
// also change the name=$id to name="'.$id.'"
echo '</td>';
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I am not sure about your javascript, but it for sure needs the change I made to it.
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
// change to-> Id from-> $id
function toggle(source, Id) {
checkboxes = document.getElementsByName(Id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<table>
<?php
for ($x = 0; $x < 3; $x++) {
$id = $x; ?>
<tr>
<td>
<!--$id won't work in single quotes like that so you have to break it out.
See comment below, but you may need to base the toggle on the class-->
<input type="checkbox" onClick="toggle(this, '<?php echo $id; ?>')" /> Toggle All
</td><?php
for ($y = 0; $y < 7; $y++){ ?>
<td>
<!-- You will likely need to array this name, because if the
names are all the same, it will only take one value -->
<input type="checkbox" name="<?php echo $id; ?>[]" class="sub-<?php echo $id; ?>" value="bar1"> Bar 1<br/>
</td><?php } ?>
</tr><?php
} ?>
</table>
I have a script written in Mysql that just lists people's names. It works fine in mysql but i have been trying to get it to update to mysqli and i got most of it. However, the results are supposed to be paginated. The pagination works fine because there should be 5 pages and thats where it stops....but ALL of the results just show up on every page. Not sure what i am doing...any help would be greatly appreciated....I know it has to do with "mysql_result"
Here is my new code in mysqli:
<?php
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = $con->query("SELECT EmployeeID,FirstName,LastName FROM employee;");
//If Database Error...Print the error and exit
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//If No Database Error.....Continue
if ($result)
{
// Return the number of rows in result set
$total_results=mysqli_num_rows($result);
//printf("Result set has %d rows.\n",$total_results);
// Free result set
}
//total pages we going to have
$total_pages = ceil($total_results / $per_page);
//if page is setcheck
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page'];//it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View All Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<img src="../admin/images/logo-thompson-industrial-services.gif" class="text- center"width="259" height="59" />
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
// close table>
echo "</table>";
?>
</div>
</div>
</div>
</div>
</body>
</html>
And here is my original code in mysql:
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_DEPRECATED);
include('config.php'); //include of db config file
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = mysql_query("SELECT * FROM employee");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<style type="text/css">
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<img src="../admin/images/logo-thompson-industrial-services.gif" class="text-center"width="259" height="59" />
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'FirstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'LastName') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
</div>
</div>
</div>
</body>
</html>
I am doing something wrong here and i cant figure it out....
Any help would be greatly appreciated...
Thanks a lot.
I would also just remove the while statement and use mysqli_data_seek in conjunction with mysqli_fetch_accoc. The MySQLi_data_seek points to the result I need. Check it out:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
//go to the column you want the results
mysqli_data_seek($result, $i);
$row = mysqli_fetch_assoc($result);
// echo out the contents of the row into a table
echo "<tr>";
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['LastName'] . '</td>';
echo "</tr>";
}
}
echo "</table>";
The way you are extracting the query is the problem. Once you enter the while statement it does not leave until all the results are done and then it executes
if ($i == $total_results) {
break;
}
So it never gets a chance to end at $i greater than $total_result. The fix is to include LIMIT and OFFSET in the query. That means you have to fix the pagination page too.
So just run the query again like:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// run the query again
$sql ="SELECT EmployeeID,FirstName,LastName FROM employee LIMIT $per_page OFFSET $end;
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
echo "</table>";