If you look at the jsfiddle you'll notice modules in yellow and red. I have all my code working except that I have to manually input the html/php for the modules.
http://jsfiddle.net/W6zYA/1/
My module code is:
<?php $row = mysql_fetch_row($result); ?>
<a href='#' data-reveal-id="<?php echo $row[0]; ?>" data-animation="none" >
<div class="grid_3">
<p id="contexttitle">
<?php echo $row[1]; ?>
<span style="float:right;"> <?php echo $row[3]; ?> </span>
</p>
<p id="accesssubmenu">Last Update: <?php echo $row[6]; ?> </p>
</div>
</a>
<div id="<?php echo $row[0]; ?>" class='reveal-modal'>
<h1> <?php echo $row[1]; ?> </h1>
<p> <?php echo $row[4]; ?> </p>
<p4>Last Update: <?php echo $row[6]; ?> </p4>
<a class="close-reveal-modal">×</a>
</div>
What I'm wanting to do is have it create X amount of these modules based on a SQL Query where I count the # of rows in each section (red, yellow, green).
I tried to store the php in the SQL database and after research found out that this is bad practice and shouldn't be done even when using eval() as there will be user input. I also read about PHP templates such as Smarty but wasn't certain if that was the correct solution for me.
If someone can point me in the right direction I'd greatly appreciate it I'm just not sure where to start/what to google.
mysql_fetch_row only fetches one row. If you have multiple rows you need mysql_fetch_assoc or mysql_fetch_array and use something like a foreach loop to loop through the results. This will output the html as many times as results in the MySQL query.
Try mysql_num_rows($result) that will give you the number of rows returned by the query. I would try to upgrade to PDO or mysql since the mysql_* functions are going to be deprecated in PHP 5.5
http://php.net/manual/en/function.mysql-num-rows.php
I assume, that you get multiple records from you database? That way you can use a while loop to loop trough your result.
while ($row = mysql_fetch_row($result)) {
// do you html code here.
}
That should do the trick.
Related
Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)
I am trying to have the title and date break to two lines but they are currently running in one line.
PHP novice here needs help!
<span class="kb-topic__list-article-title"><?php echo esc_html(get_the_title()); echo nl2br(the_date());?> </span></a>
All you need to do is echo a line break and not use the nl2br() function:
<span class="kb-topic__list-article-title">
<?php
echo esc_html(get_the_title());
echo '<br>';
echo (the_date());
?>
</span>
As long as you're jumping in and out of PHP and HTML, I would separate the dynamic content from the HTML layout. You can accomplish your goal in this fashion with something like the following:
<span class="kb-topic__list-article-title">
<div>
<?php echo esc_html(get_the_title()); ?>
</div>
<div>
<?php echo (the_date()); ?>
</div>
</span>
You can then style the HTML elements however you need to in order to achieve the exact layout you're going for. However the above code will get you the basic effect of putting the output of your two functions on separate lines.
You can also:
<span class="kb-topic__list-article-title">
<?php echo esc_html(get_the_title());?>
<br>
<?php echo (the_date());?>
</span>
have the brake not in php
I see two methods but I don't know which is better, should I insert a div tag like this :
echo "<div>";
or like this ?
?> <div> <?php
There's no right answer to this.
When displaying a lot of HTML, I normally use ?> <div> <?php. This is especially true for when looping over data from a database, I would use:
<?php while ($row == $query->fetchObject() ) : ?>
<div class="row-<?php echo $row->id ?>">
//Some more of the data
</div>
<? endwhile; ?>
However, displaying only a little, I normally just echo it.
<?php echo "Hello, my name is <strong>Joe Doe<strong>"; ?>
I would encourage you to play with it and find what you like best.
The text editor i am using has the color formatting, when you are echoing things, it only all comes out the same color, so if you are outside of PHP you get the benefit of the color co-ordination.
Yes, I've looked around and yes I've used mysql_fetch_array hundreds of times successfully before. I want a php file that will output an html file with some constants and a few interpreted php variables. After an hour, this is where I'm at.
<?php
require "(this is sql statement to get all rows from the db).php";
$i = 1;
while($row = mysqli_fetch_array($results_getpieces) && $i < 11 ){ ?>
<li>
<a href="#image-1">
<img src="<?php echo $row['content']; ?>" alt="image01">
<span><?php echo $row['title']; ?></span>
</a>
<div class="lb-overlay" id="image-1">
<img src="<?php echo $row['thumb']; ?>" alt="image01" />
<div>
<h3>blahblah <span>bluelue</h3>
<p>sherpas don't dance</p>
Prev
Next
</div>
x Close
</div>
</li>
<?php
$i++;
}
?>
When I display the $row results in a table, there's no problem. Before I added that i++ business, I could get the first record from my results, but it would just repeat those results for all ten instances.
Help almighty stackers, and overflow me with your knowledge. Please. I can't go to sleep until I get this. Where am I going wrong?
You need to parenthesize the condition appropriately, so that the array that you get back from the function call doesn't get "and"-ed with the condition "$i < 11":
($row = mysqli_fetch_array($results_getpieces)) && $i < 11
I have this PHP function, which I use to display my userdata. If run once it will basicly display a div with the userdata in it.
function listings($fornavn, $efternavn, $email, $adresse, $tlf, $postnr, $city, $fodselsdag, $brugerid,$bartype,$idbar)
{
?>
<div class="container">
<span class="records" id="id-<?php echo $brugerid;?>">
<div class="customer bidDiv clearfix">
<?php if ($bartype=='temp_vip') { ?>
<ul>
<li>
<span class="actionSpan" id="<?php echo $brugerid;?>" value="<?php echo $idbar;?>">
<a class="edit-opt" id="godkend" href="#">GODKEND</a>
</span>
</li>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Afvis</a>
</span>
</li>
<?php }else{ ?>
<ul>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Slet</a>
</span>
</li>
<li>
<a class="edit-opt" href="editform.php?id=<?php echo $brugerid."&bartype=".$bartype;?>" rel="facebox">Rediger</a>
</li>
<?php if ($bartype =='vip'){?>
<li>
<a class="print-opt" href="print.php?id=<?php echo $brugerid;?>" rel="facebox">Print</a>
</li>
<?php }else{
// Dont render vip link
}}?>
</ul>
<p class="contact-data">
<?php echo $email;?><br>
Tlf.: <?php echo $tlf;?>
</p>
<div class="base-data">
<h4><?php echo ucwords($fornavn)." ".ucwords($efternavn);?></h4>
<p>Fødselsdag <?php echo $fodselsdag;?></p>
<address><?php echo ucwords($adresse) ." ". $postnr ." ". ucwords($city);?></address>
</div>
</div><!-- end customer -->
</div>
</span>
<?php
I mostly use this function in a loop, to display all users from my MySQL database, who has a specific relation to something else.
This is all working great. But currently it would pull all results at once. Right now I only have 5 members, so its no problem, but say I get 800 or 2000 members, the list of users would get very long.
So I want to implement some kind of limit on how many users it displays, but still be able to browse through all the users. Many sites use something like pages and split up the results that way. Links like:
[1] [2] [3] [Last page>>]
I can't figure out how to start doing this? How would I proceed?
Also looked into this Jquery plugin:
http://andersonferminiano.com/jqueryscrollpagination/
But it keeps reloading my PHP file which results in the Loops being restarted, and it simply displays the results over and over again endlessly.
Can someone help me with the logic behind creating this? Or better, point me in a direction where I could use the jquery pluging above - where it only loads the loop one time, and renders the results as I scroll.
Any help or pointers will be greatly appreciated!
Thanks
Jquery pagination by default will only break-up the given table into several tabs on the page, that is all the data is actually loaded. There are some that let you do an AJAX request to fetch the next page, to work in conjunction with your PHP code.
Ideally, you want to limit the query using LIMIT (number of rows in the result object) and OFFSET (Start from row X ) which will give you number of records starting from your offset row up until the limit, and then use logic on the PHP side to determine the maximum number of pages.
$page = $_POST["page"] * 25; // make sure that page 0 is the starting page or calculate
$SQL = "SomeQueryText LIMIT 25 OFFSET '$page'" ;
$result = query ($SQL);