Currently when using the below code, I get website that looks like this:
Image here (Had to paint out some info due to GDPR)
This is the code used:
<div class="page group">
<div class="section">
<div class="col span_1_of_3">
<div id="outer_wrapper">
<div id="inner_wrapper">
<?php
$query = "SELECT * FROM sales WHERE ident ='".$currentName."' AND status ='Venter' ORDER BY STR_TO_DATE(date, '%d.%m.%Y') DESC";
if ($result = $mysqli->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if ($num_rows > 0) {
echo '<div class="box">';
echo '<div class="Nwrapper">';
echo '<div id="NformContent">';
echo
"<tr><br>
<td><b>{$row['date']}</b></td><br>
<td> {$row['name']} </td> <b>/</b>
<td>{$row['gsm']} </td><br>
<td> {$row['email']} </td><br><br>
<td> <b>Info</b> <br> {$row['pp']} </td><br>
<td>Portering: {$row['transfer']}</td><br>
<td><a href='delete.php?id={$row['id']};' class='aRS'>Oppdater status</a><a onClick=\"javascript: return confirm('Ønsker du å angre salget?');\" href='delete.php?id={$row['id']};' class='aR'>Slett salg</a></td><br><br>
</tr>";
echo '</div>';
echo '</div>';
echo '</div>';
} else {
echo "No appointments";
break;
}
}
/*freeresultset*/
$result->free();
}
?>
</div> <!-- inner_wrapper -->
</div> <!-- outer_wrapper -->
</div> <!-- col span_1_of_3 -->
</div> <!-- section -->
</div> <!-- page group -->
<div class="lineWrapper"></div><br>
What I want to do is to hide the top horizontal boxes if the query return no value(0 number of rows(?) num_rows). I have tried doing it like this, and put my whole code inside the PHP query:
<?php
$query = "SELECT * FROM sales WHERE ident ='".$currentName."' AND status ='Venter' ORDER BY STR_TO_DATE(date, '%d.%m.%Y') DESC";
if ($result = $mysqli->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if ($num_rows > 0) {
?>
<div class="page group">
<div class="section">
<div class="col span_1_of_3">
<div id="outer_wrapper">
<div id="inner_wrapper">
<?php
echo '<div class="box">';
echo '<div class="Nwrapper">';
echo '<div id="NformContent">';
echo
"<tr><br>
<td><b>{$row['date']}</b></td><br>
<td> {$row['name']} </td> <b>/</b>
<td>{$row['gsm']} </td><br>
<td> {$row['email']} </td><br><br>
<td> <b>Info</b> <br> {$row['pp']} </td><br>
<td>Portering: {$row['transfer']}</td><br>
<td><a href='delete.php?id={$row['id']};' class='aRS'>Oppdater status</a><a onClick=\"javascript: return confirm('Ønsker du å angre salget?');\" href='delete.php?id={$row['id']};' class='aR'>Slett salg</a></td><br><br>
</tr>";
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div> <!-- inner_wrapper -->
</div> <!-- outer_wrapper -->
</div> <!-- col span_1_of_3 -->
</div> <!-- section -->
</div> <!-- page group -->
<div class="lineWrapper"></div><br>
<?php
} else {
break;
}
}
/*freeresultset*/
$result->free();
}
?>
When I save, it all looks good, the top boxes are all gone, but now if the number of rows is higher then 0, and there are some value to show, it looks like this(each displays in a vertical order):
Image here
Any suggestions on how I can get this to work like I want it to?
There is while loop. Inside loop you open table row put chunk as $num_rows and close row, than goes another chunk in it's own row.
While loop works as loop - chunk after chunk - end of loop;
I'm not sure - you want them all horizontally? In one row?
Actually, you don't need any table for this, this isn't tabular content, more appropriate would be section or div or something similar.
Whatever you pick, for now it would be a 'box';
this is how it should go:
open a box,
query, fetch - if is empty do nothing {}
if isn't empty - write down chunk after chunk,
close box;
With no chunks it will be empty box - you can hide it with .css (or not...)
Other solution is to assign all chunks to variable $x.= chunk
and then - if (x) is not empty - open a box - write it (x) down - close box;
If the output (x) is big, may cause problem.
Next one solution - workaround :
$box = '<div>'; // echoing $box will open a box
query, fetch, if empty do nothing;
if not empty output - echo $box write first chunk, unset $box - following chunks won't echoing $box,
after loop, if $num_rows > 0 close box (if 0 rows there is no open box)
Related
I am displaying information in a card-based layout, but if any title in the card is longer than 41 characters, it doesn't fit into the card.
I thought about using [wordwrap], but all that would do is cause the one specific title to wrap and change the height of that card, affecting the layout of the other cards.
This is the PHP I use to print my results for pagination purposes, so it only prints 9 things at a time.
<div class="row">
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><? echo $row["title"]; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
</div>
How would I go about line breaking every title if even one title is detected as longer than 41 characters?
EDIT: This is the solution I created:
$titlebreak = $row["title"];
if (strlen($titlebreak) >= 40)
{
$titlebreak2 = wordwrap($titlebreak, 39, "</p><p>");
}
else
{
$titlebreak2 = $row["title"] . "<p> </p>\n";
}
I've included 3 possible solutions below - adding manual line breaks as you asked about in your question; a basic but unsatisfactory CSS option and a jQuery solution which in this case is the one I would suggest as the most flexible.
Although a CSS-only solution is usually the preferred way of fixing a layout issue, when it comes to equal heights of elements there isn't a clear-cut way to do it and often a jQuery solution like the one below is required.
Manual line-break - as requested in your question
Instead of doing an additional SQL query as mentioned, you can easily do it in the PHP in 2 different ways:
(a) loop through the rows before displaying them to calculate the title lengths, then loop again to display with/without the line break
or
(b) if you really down't want to loop twice, you could include the line break regardless of length as you loop once, but also calculate the line length in that loop. Then hide the line break using CSS if its not required
(a) 2 loops: Calculate length to determine whether to add the line break or not:
<?php
$maxchars = 41;
$cards = array();
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
// save detaisl to $cards array
$cards[$row["title"]] = $row["category"];
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
}
?>
<div class="row">
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><?
// if there were any long title, wrap them all text
if ($bLongTitle)
$title = wordwrap($title, $maxchars, "<br />\n");
echo $title;
?></p>
<p>Category: <? echo $category; ?></p>
</div>
</div>
</div>
<?php
}
?>
</div>
(b) 1 loop: Always display line break, and hide it if not required
<div class="row">
<?php
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle"><? echo wordwrap($title, $maxchars, "<br />\n"); ?></p>
<p>Category: <? echo $row["category"]; ?></p>
<?php
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
?>
</div>
</div>
</div>
<?php
};
?>
</div>
<?php if ($bLongTitle) {?>
<script>.cardTitle br {display:none;}</script>
<?php } ?>
CSS-only solution - not viable for the OP?
Because the titles are aren't direct siblings, the only way would be to fix the height of all title. This isn't a desirable solution, as I'm sure titles can vary a lot in length so its impossible to pick a "default" height to suit every possibility, and even that's complicated by the responsive width of the columns potentially changing the heights dynamically.
But for the sake of completeness:
add a class (e.g. .cardTitle) to the title in your loop
identify suitable heights for the title with and without a line break, and set these in your CSS
add the corresponding class (e.g. wrapTitle) to your <p> if any title is too long in a loop (similar to adding a line break above)
CSS
p.cardTitle { height:20px; } /* example of the default height for title */
p.cardTitle.wraptitle { height:40px; } /* example of height for wrapped title */
PHP (after looping through SQL rows to fill $cards array as option (a) above)
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle <?php if ($bLongTitle) echo "wrapTitle"; ?>"><? echo $title; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
jQuery
You could use jQuery to loop through all elements to calculate the heights, and set them all to the tallest.
You could write the code yourself (see How to make all div columns of the same height automatically with jQuery or Setting equal heights for div's with jQuery but there is a library available to do this for you: matchHeight
Using the library, all you need to do is include it on your page and call it like this (assuming you've added the class cardTitle to the <p> that holds your title)
jQuery(document).ready(function() {
$(".cardTitle").matchHeight();
});
I would perform a new SQL query first to see if any results have a title length of greater than 41 characters:
select count(*) from table where length(title)>41
And then set the result of this query to be a variable, e.g. $has41
You can then use an if statement within your loop ...
if($has41) {
// do something with $row['title']
} else {
// do something else with $row['title']
}
I am attempting to echo out a JQM table in viewsessions.php file. I have researched the topic and tried this Jquery Mobile Table in PHP code solution, but the page does not load with my code, so I have stripped it back to where the table works.
Any ideas how to do this?
viewsessions.php
<?php
$servername = "";
$username = "";
$password = "";
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill'
$con=mysqli_connect('localhost','root','cornwall','ibill');
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This is the query that is sent to the ibill database.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Type of Activity</th>
<th>Employer</th>
<th>Date</th>
<th>Time</th>
<th>Amount (GBP)</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["typeofactivity"]."</td>
<td>".$row["employer"]."</td>
<td>".$row["date"]."</td>
<td>".$row["time"]."</td>
<td>".$row["amount"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$con->close();
?>
I am using multiple pages inside one html file so have just posted the page with the relevant code below..
main.php#viewsessions
<!--**********************************************VIEW SESSIONS PAGE*******************************************************************************-->
<!--***********************************************************************************************************************************************-->
<!--********************************HEADER**************************************************************************************-->
<div data-role="page" id="viewsessions">
<div data-role="header" data-id="foo1" data-position="fixed">
<div class='cssmenu'>
<ul>
<li class='active'><a href='#home'>Home</a></li>
<li><a href='#sessionrecord'>Record a Session</a></li>
<li><a href='#viewsessions'>View Sessions</a></li>
<li><a href='#invoice'>Create an Invoice</a></li>
</ul>
</div>
</div><!-- /header -->
<!--********************************HEADER***************************************************************************************-->
<!--********************************MAIN*****************************************************************************************-->
<div data-role="main" class="ui-content">
<p><span class="logout">Sign Out</span></p>
<div class="loginphp">
<?php
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
echo '<p>Signed in as: <span class="uname">' . $_SESSION['user_session'] . '</span></p>'; }
else {
echo 'not working';
}
?>
</div>
<img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="200" height="152">
<h1>Recorded Sessions</h1>
<section class="maincontent">
<?php
include "viewsessions.php";
?>
</section>
</div>
<!--********************************MAIN******************************************************************************************-->
<!--********************************FOOTER****************************************************************************************-->
<div data-role="footer">
<footer class="footer">
<p>awilliams©</p>
</footer>
</div>
</div>
<!--********************************FOOTER*****************************************************************************************-->
<!--**********************************************************END OF VIEW SESSIONS PAGE***************************************************************-->
<!--**************************************************************************************************************************************************-->
if you are updating table from server you need to rebuild it using ( ".selector" ).table( "rebuild" );
otherwise you might just need to reinitialize the table
$( ".selector" ).table({
defaults: true
});
moreover you need to include data-role="table" attribute in your table tag
and i think that might be your problem cause you did not insert it
try to include that tag , and if it didn't work use the below code
if ($result->num_rows > 0) {
echo '<table data-role="table" class="selector" data-mode="reflow"> <tr>
<th>Type of Activity</th>
<th>Employer</th>
<th>Date</th>
<th>Time</th>
<th>Amount (GBP)</th>
</tr>';
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["typeofactivity"]."</td>
<td>".$row["employer"]."</td>
<td>".$row["date"]."</td>
<td>".$row["time"]."</td>
<td>".$row["amount"]."</td>
</tr>";
}
echo '</table><script> $( ".selector" ).table({
defaults: true
});</script>';
} else {
echo "0 results";
}
You PHP code looks OK, and so do your HTML. Note that if your PHP code works correct, the table will be generated as a part of the HTML document before the jQuery Mobile styling and enhancment. Please check that your PHP code actually returns the desired HTML code. This can be done by copy/paste the code to a separate script and run it directly.
Also consider to add the following check for good practice. The reason is that $con->query() might return FALSE, while $result->num_rows expects a mysqli_result object.
// This is the query that is sent to the ibill database.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);
//Check if a result was returned
if($result === FALSE){
echo mysqli_error($con);
}
This one is going to be a bit hard to explain but I am going to try my best.
I have a database with a table called content with 3 columns, I’m trying to get the values as rows and put them in different <div>’s. here is what I wrote so far
<?php
$sql = "SELECT * FROM content";
$query = mysqli_query($db, $sql) or die (mysqli_error());
$contentDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$content_id = $row["id"];
$content_title= $row["title"];
$content_text = $row["text"];
$contentDisplay .= '<h1>'.$content_title.'</h1> <p>'.$content_text.'</p>' ."\n";
}
$row_cnt = $query->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>
The row count gives 4 rows which is correct amount of rows.
In my html I have <?php echo $contentDisplay; ?> which puts out all 4 rows after each other, but I need to show the first row in my first <div>, the second row in my second <div> and so forth. thanks in advance
Update: I forgot to say that I have my <div>'s in the html part with different styles. all i need is the first row in one <div> (like on top of the page) and second row in another <div> (like on the bottom of the page) :)
Update2: here is the html code
<div class="content-top">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-left">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-center">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-right">
<section>
<h1></h1>
<p></p>
</section>
</div>
i want the first row to be displayed in <div class="content-top"> the second row in <div class="content-left"> the third row in <div class="content-center"> and the fourth row in <div class="content-right">
<?php
$sql = "SELECT * FROM content";
$query = mysqli_query($db, $sql) or die (mysqli_error());
$rows = [];
while ($row = mysqli_fetch_array($query))
$rows[] = '<h1>'.$row['title'].'</h1> <p>'.$row['text'].'</p>' . "\n";
?>
<div class="content-top">
<section>
<?php echo $rows[0]; ?>
</section>
</div>
<div class="content-left">
<section>
<?php echo $rows[1]; ?>
</section>
</div>
<div class="content-center">
<section>
<?php echo $rows[2]; ?>
</section>
</div>
<div class="content-right">
<section>
<?php echo $rows[3]; ?>
</section>
</div>
$contentDisplay .= '<div><h1>'.$content_title.'</h1> <p>'.$content_text.'</p></div>' ."\n";
Just add divs for each row? :) This way, each row in the database will get a div.
Each row will be appended to the $contentDisplay variable, but since all you are doing is printing it, you can print the html directly.
I'm not sure why you are being downvoted, since you are asking a valid question with code that shows what you have tried so far.
Okay, so I got this site im working on to better myself with php, And I got a category system working in a switch and also a pagination, but i can't really get it to work together.
What I got so far graps all 4 posts from my DB and displays it on the "blog.php" page with the pagination making it only show 1 result at a time, blog.php?page=2 showing the next and so on, and it all works great.
Now clicking on the category "test" will display 2 results on the page "blog.php?category=2", still showing 1 by the pagination, but clicking on the next page "blog.php?category=2?page=2" will cause it to give the warnings
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\rusting\blog.php on line 284
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\rusting\blog.php on line 325
The code i got is this
<?php switch($category):
case 0: ?>
<?php
//Fetch from database first 1 items which is its limit. For that when page open you can see first 10 items.
$query=mysqli_query($db,"
SELECT blog.id, blog.title, blog.date, blog.content, blog.image, blog.author_id, blog.category, blog.short_desc, category.id, category.name
FROM blog
INNER JOIN category
ON blog.category=category.id
ORDER BY blog.id DESC
LIMIT $start, $limit
");
//print 1 items
while($result=mysqli_fetch_array($query))
{
echo "
<!-- Begin Post -->
<div class='post'>
<!-- Begin Post Info -->
<div class='post-info'>
<!-- Begin Date -->
<div class='post-date'> <span class='day'>15</span> <span class='month'>FEB</span> <span class='year'>2011</span> </div>
<!-- End Date -->
<!-- Begin Title -->
<div class='post-title'>
<h1><a href='post.php?id=".$result['id']."'>
".$result['title']."
</a></h1>
<div class='post-meta'> <span class='comments'>13 Comments</span> <span class='categories'><a href='#'>".$result['name']."</a></span> </div>
</div>
<!-- End Title -->
</div>
<!-- End Post Info -->
<div class='post-text'>
<div class='post-img'><a href='post.php?id=".$result['id']."'><img src='style/images/blog/".$result['image']."' alt='' /></a></div><br/>
<p>".$result['short_desc']." <a class='more' href='post.php?id=".$result['id']."'>Læs mere →</a></p>
</div>
<!-- End Text -->
</div>
<!-- End Post -->
";
}
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($db,"select * from blog"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
?>
<br><br><br><br>
<?php if($page>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<li><a href='?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
////Go to previous page to show next 10 items.
echo "<li><a href='?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>
<br><br><br><br>
<div class="page-navi">
<ul>
<?php
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"; }
}
?>
</ul>
</div>
<?php break;?>
<?php case $category: ?>
<?php
//Fetch from database first 1 items which is its limit. For that when page open you can see first 10 items.
$query=mysqli_query($db,"
SELECT blog.id, blog.title, blog.date, blog.content, blog.image, blog.author_id, blog.category, blog.short_desc, category.id, category.name
FROM blog
INNER JOIN category
ON blog.category=category.id
WHERE category=".$category."
ORDER BY blog.id DESC
LIMIT $start, $limit
");
//print 1 items
while($result=mysqli_fetch_array($query))
{
echo "
<!-- Begin Post -->
<div class='post'>
<!-- Begin Post Info -->
<div class='post-info'>
<!-- Begin Date -->
<div class='post-date'> <span class='day'>15</span> <span class='month'>FEB</span> <span class='year'>2011</span> </div>
<!-- End Date -->
<!-- Begin Title -->
<div class='post-title'>
<h1><a href='post.php?id=".$result['id']."'>
".$result['title']."
</a></h1>
<div class='post-meta'> <span class='comments'>13 Comments</span> <span class='categories'><a href='#'>".$result['name']."</a></span> </div>
</div>
<!-- End Title -->
</div>
<!-- End Post Info -->
<div class='post-text'>
<div class='post-img'><a href='post.php?id=".$result['id']."'><img src='style/images/blog/".$result['image']."' alt='' /></a></div><br/>
<p>".$result['short_desc']." <a class='more' href='post.php?id=".$result['id']."'>Læs mere →</a></p>
</div>
<!-- End Text -->
</div>
<!-- End Post -->
";
}
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($db,"select * from blog WHERE category=".$category.""));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
?>
<br><br><br><br>
<?php if($page>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<li><a href='?category=".$category."?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
////Go to previous page to show next 10 items.
echo "<li><a href='?category=".$category."?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>
<br><br><br><br>
<div class="page-navi">
<ul>
<?php
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
else { echo "<li><a href='?category=".$category."?page=".$i."'>".$i."</a></li>"; }
}
?>
</ul>
</div>
<?php break;?>
<?php endswitch;?>
Okay, it was pretty simple and i can't believe i didn't realize this earlier. I just needed to change the link generated for it to work.
I was trying to visit page 2 on category 2 with "blog.php?category=2?page=2"
For it all to work i just needed to change it to "blog.php?category=2&page=2"
I have a dynamically generated DIV
<div class="content drag-desired">
<?php
$result = mysql_query("SELECT * FROM XXXX WHERE qty != 0");
while($row=mysql_fetch_assoc($result))
{
echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" />
<div>'.$row['price'].'$</div></div>';
}
?>
<div class="clear"></div>
</div>
the while loop makes the list be very long,
Any idea how to make the div contains 6 items only and show the fetched items 6 by 6?
I don't know the logic behind the scene. :)
I will be appreciated, if someone explain the follow chart for making the div slides.
Thanks
Like so:
<div class="content drag-desired">
<?php
$result = mysql_query("SELECT * FROM XXXX WHERE qty != 0");
$counter = 0;
while($row=mysql_fetch_assoc($result))
{
if($counter==0)
echo '<div class="slide">';
echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" />
<div>'.$row['price'].'$</div></div>';
if($counter==5)
echo '<div>';
$counter++;
if($counter > 5)
$counter = 0;
}
?>
<div class="clear"></div>
</div>
So, the code below counts to 6 and wraps 6 items inside "slide". I think you understand the logic I used :) And with a little hint of CSS/JavaScript you can make your own slider that changes slide to be shown.