php echo page numbers in same line - php

i have pagination system on my echo results however, the page numbers are being echo'ed underneath eachother
e.g.
1
2 3 4 5
this is my code:
for($i=0;$i < $count1;$i=$i+$limit)
{
if($i <> $start)
{
echo "<a href='view.php?search=$search&start=$i&limit=$limit&price=$price&category=$category'><font face='Verdana' size='2'><b> $l </b></font></a> ";
}
else
{
echo "<center><font face='Verdana' size='4' color=#2E9AFE ><b> $l </b></font></center>";
}
MODIFIED CODE:
$i=0;
$l=1;
echo "<p align='left'>";
for($i=0;$i < $count1;$i=$i+$limit)
{
if($i <> $start)
{
echo '$i';
}
else
{
echo '<span class="current">$i</span>';
}
$l=$l+1;
}
echo "</p>";
}

<?php for($i=0;$i < $count1;$i=$i+$limit): ?>
<?php if($i <> $start): ?>
<a href="view.php?search=<?php echo $search;?>&start=<?php echo $i;?>&limit=<?php echo $limit;?>&price=<?php echo $price;?>&category=<?php echo $category;?>">
<?php echo $i; ?></a>
<?php else: ?>
<span class="current"><?php echo $i;?></span>
<?php endif; ?>
<?php endfor; ?>
CSS:
a,span.current{font-weight:bold;font-family:Verdana;}
All are inline elements, so they'll stay...inline. Add paddings, text-decoration, colors and whatever in your CSS. I could help you to achieve exactly what you want if you are more clear in your intentions (center?). Stay away from old html and tables :)

Here is another way of doing the above, with a few added extras:
$count1 = 10;
$menu = '';
$link = array(
'search' => ( isset($search) ? $search : $search = '' ),
'start' => ( isset($start) ? $start : $start = 0 ),
'limit' => ( isset($limit) ? $limit : $limit = 1 ),
'price' => ( isset($price) ? $price : $price = '' ),
'category' => ( isset($category) ? $category : $category = '' ),
);
foreach( range(0, $count1, $limit) as $i ) {
$menu .= ( ($link['start'] = $i) == $start ?
'<span class="current">'.$i.'</span>' :
''.$i.''
) . PHP_EOL;
}
echo $menu;
(the above requires php5+)
With regards to neeko's comment, to seperate the $start variable from the text displayed in the link - all you have to do is either introduce another variable that counts up with each loop - or, as we already have the key of the range array, we can use that:
foreach( range(0, $count1, $limit) as $key => $i ) {
$menu .= ( ($link['start'] = $i) == $start ?
'<span class="current">Page'.($key+1).'</span>' :
'Page'.($key+1).''
) . PHP_EOL;
}
I've used $key + 1, because $key will be zero-based (i.e. counting up from Zero) but the +1 just shifts things so we count up from 1 instead.

I think it is because center is a block element which means it won't be inline with the other numbers (but that isn't my specialty so I might be wrong. You could put them in a table.
echo "<table><tr>";
for($i=0;$i < $count1;$i=$i+$limit)
{
echo "<td>";
if($i <> $start)
{
echo "<a href='view.php?search=$search&start=$i&limit=$limit&price=$price&category=$category'><font face='Verdana' size='2'><b> $l </b></font></a> ";
}
else
{
echo "<center><font face='Verdana' size='4' color=#2E9AFE ><b> $l </b></font></center>";
}
echo "</td>";
}
echo "</tr></table>";

Try this:
for($i=0;$i < $count1;$i=$i+$limit)
{
if($i <> $start)
{
echo "<a href='view.php?search=$search&start=$i&limit=$limit&price=$price&category=$category'><div style='font-family: verdana; font-size: 12px; font-weight: bold; float: left; position: relative; padding: 5px; margin-right: 5px;'>$l</div></a>";
}
else
{
echo "<div style='font-family: verdana; font-size: 14px; font-weight: bold; float: left; position: relative; color: #2E9AFE; padding: 5px; margin-right: 5px;'>$l</div>";
}
I have changed your html part of code to display in div elements with float: left and some other css tweaks so now it should be placed in one line...

Related

How to freeze column 1 and scroll through other columns and rows in html with php and sql fields

I have the following php generated table (drawing from sql database values) in which I want the first column (all the usernames) to be frozen (fixed) while the user can scroll horizontally and 'right' across the page to view the scores for each user.
I cannot figure out how to do this with the mix of php and html going on. Can anyone suggest a viable solution please?
An image for illustration purposes is below
(I wish for the username column (all rows) to be fixed) while scrolling through to the right - there will be many more quizzes)
PHP and HTML Code
<tbody>
<?php foreach(#$result as $record){?>
<tr>
<td><?php echo $record["username"];?></td>
<?php
$counter=0;
while(mysqli_num_rows($all_quizes) > $counter){
$current_td = mysqli_query($con,"SELECT * FROM quiz_takers WHERE username='".$record["username"]."' AND quiz_id=".$quizes[$counter][0]." ORDER BY marks DESC");
$td = mysqli_fetch_array($current_td);
if($td['percentage'] == null){
echo "<td>-</td>";
}else{
if(intval($td["percentage"]) >= 0 && intval($td["percentage"]) <= 30){
$color = 'red';
}elseif(intval($td["percentage"]) > 30 && intval($td["percentage"]) <= 70){
$color = '#ffbf00';
}elseif(intval($td["percentage"]) <= 30){
}else{
$color = 'green';
}
echo "<td style='color:".$color."'>".round($td["percentage"],2)."%</td>";
}
$counter++;
}
/*foreach($current_td as $td){
echo $counter." ".$td['username'] . " - ".$quizes[$counter]['3']."<br>";
if($quizes[$counter]['0'] == $td['quiz_id']){
?>
<td><?php echo $td["percentage"];?></td>
<?php } $counter++;}*/ ?>
</tr>
<?php } ?>
</tbody>
UPDATE:
What I have tried, thanks to the helpful suggestion below regarding CSS is the following: It works, but not quite (messed up formatting)
CSS
<style type="text/css">
.headcol {
position: absolute;
width: 5em;
left: 0;
top: auto;
border-top-width: 1px;
/*only relevant for first row*/
margin-top: -1px;
/*compensate for top border*/
}
.headcol:before {
content: 'Row ';
}
</style>
Added this line below:
<tbody>
<?php foreach(#$result as $record){?>
<tr>
<td><th class="headcol"><?php echo $record["username"];?></th></td>
<?php
$counter=0;
while(mysqli_num_rows($all_quizes) > $counter){
$current_td = mysqli_query($con,"SELECT * FROM quiz_takers WHERE username='".$record["username"]."' AND quiz_id=".$quizes[$counter][0]." ORDER BY marks DESC");
$td = mysqli_fetch_array($current_td);
if($td['percentage'] == null){
echo "<td>-</td>";
}else{
if(intval($td["percentage"]) >= 0 && intval($td["percentage"]) <= 30){
$color = 'red';
}elseif(intval($td["percentage"]) > 30 && intval($td["percentage"]) <= 70){
$color = '#ffbf00';
}elseif(intval($td["percentage"]) <= 30){
}else{
$color = 'green';
}
echo "<td style='color:".$color."'>".round($td["percentage"],2)."%</td>";
}
$counter++;
}
/*foreach($current_td as $td){
echo $counter." ".$td['username'] . " - ".$quizes[$counter]['3']."<br>";
if($quizes[$counter]['0'] == $td['quiz_id']){
?>
<td><?php echo $td["percentage"];?></td>
<?php } $counter++;}*/ ?>
</tr>
<?php } ?>
</tbody>
HTML Result is skewed: comes up with this
instead of the desired:

PHP pagination page list adjustment using CSS

I have created following pagination (see image);
here I want to display "Next>>" and "Last" at the end of 28. I cannot change the code to achieve that order because I am using if else block in my PHP code;
Here is the code part;
echo "<ul class='pagination'>";
echo "Page: $page of $last <br>";
echo "Record count: ". $rec_count. "<br>";
echo "Left count: ". $left_rec. "<br>";
echo "Record limit: ". $rec_limit. "<br>";
if( ($page==1) && ($left_rec>0) ){ //this is first page
echo '<li> Next >> <br> </li>';
echo "<li> Last </li>";
}
else if( ($page>1) && ($left_rec>0) ){
echo "<li> First <br> </li>";
echo '<li> << Previous <br> </li>';
echo '<li> Next >> <br> </li>';
echo "<li> Last </li>";
}
else if( ($page>1) && ($left_rec<$rec_limit) ){
echo "<li> First </li>";
echo '<li> << Previous <br> </li>';
}
/* Displaying page numbers (optional) */
for( $i=1; $i<=$last; $i++ ){
if( $i==$page ){
echo "<li> <a class=\"current\" href=\"{$_SERVER['PHP_SELF']}?page=$i\"> $i </a> </li>";
}
else{
echo "<li> $i </li>";
}
}
echo "</ul>";
How can I achieve that using CSS? I have my CSS used so far:
<style>
ul.pagination{
text-align:center;
color:#829994;
}
ul.pagination li{
display:inline;
padding: 0px 3px; /* here we cannot specify padding for top and bottom because inline display does not support margins and
paddings top and bottom*/
}
ul.pagination a{
color:#0d7963;
display:inline-block;
border:1px solid #cde0dc;
margin:3px 0;
padding:5px 10px;
text-decoration: none;
}
ul.pagination a:hover,
ul.pagination a.current{
background:#0d7963;
color:#fff;
}
</style>

PHP shopping cart does not work right

My shopping cart does not function right.
If i add one item to the cart it works accordingly, but when i add a second or multiple items, the cart seems act strange...
When adding a second item, the quantity of the second item starts at 2.
The quantity of a newly added item starts with the amount of items pressent in the cart. It also increments with the amount of items in the cart.
After adding a third item, the item is displayed as many times as there are items it the cart...
How can change it to add a single item and only increment by one if the items is existing in the cart?
Thanks in advance!
<?php
require "core.inc.php";
require "connect.inc.php";
?>
<html>
<head>
<meta charset="UTF-8">
<title>Winkelwagen</title>
<link rel="stylesheet" type="text/css" href="winkelwagen.css" />
<link rel="stylesheet" type="text/css" href="topAndMenuHeader.css" />
</head>
<body bgcolor="#8A8B93">
<div id="container">
<?php include "topAndMenuHeader.php"; ?>
<div id="content">
<p class="opmaakTitel">Winkelwagen</p>
<?php
if ( isset($_GET['itemID']) ) {
$itemID = $_GET['itemID'];
$sql = "SELECT * FROM items WHERE id=$itemID";
if ( $query = mysql_query($sql) ) {
$numRows = mysql_num_rows($query);
if ( $numRows == 1 ) {
$artID = mysql_result($query, 0, 'id');
$artNaam = mysql_result($query, 0, 'artNaam');
$artPrijs = mysql_result($query, 0, 'artPrijs');
$artAfbeelding = mysql_result($query, 0, 'artAfbeelding');
$artAantal = 1;
$index = -1;
if ( isset($_SESSION['cart']) ) {
unserialize(serialize($_SESSION['cart']));
for ( $i = 0; $i < count($_SESSION['cart']); $i++ )
{
if ( $_SESSION['cart'][$i]['id'] ==
$_GET['itemID'] ) {
$index = $i;
}
if ( $index == -1 ) {
array_push($_SESSION['cart'],
array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs,
'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal));
} else {
$_SESSION['cart'][$index]['aantal']++;
}
}
} else {
$_SESSION['cart'] []=array('id'=>$artID,
'naam'=>$artNaam,'prijs'=>$artPrijs,
'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal);
}
}
}
}
foreach ( $_SESSION['cart'] as $cart ) {
echo '<div id="artikelSpace">';
echo '<div id="artikel">';
echo '<div id="afbeelding">';
echo'<imgsrc="data:image/jpeg;base64,'
.base64_encode($cart['afbeelding']).'" />';
echo '</div>';
echo '<div id="naam">';
echo '<p>'.$cart['naam'].'</p>';
echo '</div>';
echo '<div id="aantal">';
echo '<form action="winkelwagen.php" method="POST">';
echo 'Aantal:';
echo '<input type="text" name="aantal" value="'
.$cart['aantal'].'" style="width: 50px; margin: 0px 20px 0px 20px" />';
echo '<input type="submit" name="wijzigAantal"
value="Wijzig" style="width: 100px;
border-radius: 5px; font-weight: bold;" />';
echo '</form>';
echo '</div>';
echo '<div id="prijs">';
echo '<p>Prijs: €'.($cart['prijs'] * $cart['aantal']).'</p>';
echo '</div>';
echo '<div id="verwijder">';
echo '<ul>';
echo '<li>Verwijder</li>';
echo '</ul>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
</body>
</html>
You have to initialize the $index variable in each iteration of the loop, like this:
// your code
if ( isset($_SESSION['cart']) ) {
unserialize(serialize($_SESSION['cart']));
for ( $i = 0; $i < count($_SESSION['cart']); $i++ ) {
$index = -1; // initialize $index in each iteration
if ( $_SESSION['cart'][$i]['id'] == $_GET['itemID'] ) {
$index = $i;
}
if ( $index == -1 ) {
array_push($_SESSION['cart'], array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal));
} else {
$_SESSION['cart'][$index]['aantal']++;
}
}
} else {
$_SESSION['cart'][]=array('id'=>$artID, 'naam'=>$artNaam,'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal);
}
// your code
Sidenote: Don't use mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Matrix creation failed in PHP

I am trying to build a 4x4 matrix like structure in PHP using HTML table.
There can be n number of elements. So calling it 4x4 matrix is kinda appropriate. There can be any number of rows. But a row can have only 4 columns.
Here's what I am trying to make.
Fiddle for this structure is here.
And this is what I am getting as output.
PHP Code:
<table class="tablematrix content table-striped">
<?php
$total=7;
if($total%4==0)
{
$tr=$total/4;
}
else
{
$tr_temp=$total/4;
$tr=$tr_temp+1;
}
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=$total;$j++)
{
echo '<td>'.$j.'</td>';
}
echo '</tr>';
}
?>
</table>
CSS
<style>
.tablematrix {
border-collapse:collapse;
table-layout:fixed;
}
.tablematrix * {
height:50px;
width:50px;
min-width:50px;
min-height:50px;
margin:0px;
padding:0px;
}
.tablematrix th,
.tablematrix td {
text-align: center;
border: 1px solid #dddddd;
}
.tablematrix th {
font-weight: bold;
}
.tablematrix tbody > tr:nth-child(odd) > td,
.tablematrix tbody > tr:nth-child(odd) > th {
background-color: #f9f9f9;
}
</style>
Output here
I am kinda stuck here. Don't know how to proceed. Any suggestions?
This code should work
<table class="tablematrix content table-striped">
<?php
$total=7;
$tr=$total;
$count=1;
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=4;$j++)
{
echo '<td>'.$count.'</td>';
$count=$count+1;
}
echo '</tr>';
}
?>
</table>
Please try this. You are making a logical mistake. I didn't test my code. So let me know if my code is not working. Thanks.
You can check my ideone here https://ideone.com/STHC7H. Its showing your desired output. So as you said $total is the total number of elements instead of total number of rows the following code will give you correct result
<?php
$total=7;
if($total%4==0)
{
$tr=$total/4;
}
else
{
$tr_temp=$total/4;
$tr=$tr_temp+1;
}
$count=1;
for($i=1;$i<=$tr;$i++)
{
echo '<tr>';
for($j=1;$j<=4;$j++)
{
if($count==$total+1)
break;
echo '<td>'.$count.'</td>';
$count=$count+1;
}
echo '</tr>';
}
?>
I hope it will solve your problem
Check this code:
<table class="tablematrix content table-striped">
<?php
$row=6;
$col=4;
$total=$row*$col;
echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%$col==0)
{ echo '</tr>';
if($i!=$total)
echo '<tr>';
}
}
?>
</table>
OR
<table class="tablematrix content table-striped">
<?php
$total=24;
echo "<tr>";
for($i=1;$i<=$total;$i++)
{
echo "<td>".$i."</td>";
if($i%4==0)
{ echo '</tr>';
if($i!=$total)
echo '<tr>';
}
}
?>
</table>
Amount of columns = "" => 4.
Amount of Rows = "" = ?
Look at this part of your code
echo '<tr>';
for($j=1;$j<=$total;$j++)
{
echo '<td>'.$j.'</td>';
}
echo '</tr>';
you have set $total to 7 so you are runing 7 times and creating 7 columns instead of row which would have made more sense so basically you need to transpose your matrix
Here you have:
<?php
$rows = 6;
$cols = 4;
for ($i = 1; $i <= $rows; $i++) {
echo '<tr>';
for ($l = 1; $l <= $cols; $l++) {
echo '<td>'.((($i - 1) * $cols) + $l).'</td>';
}
echo '</tr>';
}
?>
Tested, should do the job..
You can use this function. It can be optimised not to use $tmp variable
function createHtmlMatrix ($w, $h) {
$matrixHtml = '<table class="tablematrix content table-striped">';
$tmp = 0;
for ($i = 0 ; $i < $h ; ++$i) {
$matrixHtml .= '<tr>';
for ($j = 0 ; $j < $w ; ++$j) {
$matrixHtml .= '<td>'.(++$tmp).'</td>';
}
$matrixHtml .= '</tr>';
}
$matrixHtml .= '</table>';
return $matrixHtml;
}
echo createHtmlMatrix(4,5);
try this
<?php
$row = 6;
$column = 3;
echo "<table style = 'border: 1px solid black'>";
for($x= 0; $x<= $row; $x++){
echo "<tr>";
for($y=0; $y<= $column; $y++){
echo "<td>";
echo $y;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>

Creating tables with controllable values in PHP

Now i have the following:
<?php include 'header.php'; ?>
<body>
<?php
$rows_max = 10;
$columns_max = 10;
$links = Array(
'link' => "http://testlink.com",
'image' => "img100x100.png");
print '<table border="1px" style="border-collapse:collapse;">';
for($row = 1; $row <= $rows_max; $row++)
{
print '<tr>';
for($col = 1; $col <= $columns_max; $col++)
{
print '<td width="100px" height="100px" background="'.$links["image"].'" >';
print '<center> </center>';
//print "$row - $col";
print '</td>';
}
print '</tr>';
}
print '</table>';
include 'footer.php'; ?>
AND
td a
{
width: 100%;
height: 100%;
display: block;
margin-top: 0px auto;
}
All TD-s now link and have image, although they are all the same. How should i create backend so i can change all these data values individually? Thanks!
<?php
for ($tr=0; $tr<10; $tr++) {
echo "<tr>";
for ($td=0; $td<10; $td++) {
echo "<td width=\"10\" height=\"10\">".$td."</td>";
}
echo "</tr>";
}
?>
maybe something like this?
<?php
for($tr=0;$tr<=10;$tr++)
{
echo "<tr>";
for($td=0;$td<=10;$td++)
{
$image_link = "image_link";
$image_path = "path/to/image.jpg";
echo "<td width=\"10\" height=\"10\"><img src=\"{$image_path}\" /></td>";
}
echo "</tr>";
}
?>
That is a not bad start but, you will want to structure your code to be a nice as possible so when you want to change it you aren't straining your eyes to find the part to change.
<?php
$rows_max = 10;
$columns_max = 10;
// Start Table
print '<table style="">';
for($row = 1; $row <= $rows_max; $row++) // Start at 1 instead of 0 to be nice
{
print '<tr>';
for($col = 1; $col <= $columns_max; $col++)
{
print '<td>';
print "I am in row $row and column $col";
print '</td>';
}
print '</tr>';
}
print '</table>';

Categories