I'm creating a movie database for private purposes. As a part of the database I want to be able to show the movies by sorting by genre, starting letter of movies or just show all.
I want to display the hits in a three column table until all rows in the database table have been printed.
For example:
Ice age 1 Ice age 2 Ice age 3
Die Hard 1 Die Hard 2 Die hard 3
What happens with the code below (Which is used when all movies should be shown) is that it stops after 39 rows even though I know it's 50 rows, mysql_num_rows() returns 50 and when I just printed the complete db table in 1 column I got 50 rows.
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center">Go back</p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
while ($counterone < mysql_num_rows($movieresult))
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '' . $result['title'] . '';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}
You're not incrementing $counterone in the right spot. You're counting table ROWS, but are working off number of records. $counterone should be inside the internal while($result) loop. And once it's there, $countertwo is redundant.
Try this instead:
$counter = 0;
while($row = mysql_fetch_array($movieresult)) {
if ($counter % 3 == 0)
echo '<tr>';
}
echo "<td> blah blah blah </td>";
if ($counter % 3 == 2) {
echo '</tr>';
}
$counter++;
}
Try this:
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center">Go back</p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
$numrows = mysql_num_rows($movieresult);
while ($counterone < $numrows)
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '' . $result['title'] . '';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}
Related
My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
$q2 = mysql_query("SELECT * FROM artwork LEFT JOIN folder ON folder.folder_id=artwork.folder_id WHERE id IN(0".$userids.")");
while($row = mysql_fetch_array($q2)){
$link .= '<td align="center"><a href="/art/'.$row['id'].'" title="'.$row['name'].'">
<img src="/img/artwork/'.$row['folder'].'/'.$row['file'].'" height="80" /></a><br />
<span align="center">[remove] [view]</span></td>';
}
link is getting echo'd out with one td, but I need to make it so after every 4 mysql entries it adds a tr
Just maintain a counter and test if it is divisible by 4:
$counter = 0;
while ($row = mysql_fetch_array($q2)) {
if ($counter%4 === 0) $link .= "<tr>";
$link .= "<td>...</td>";
if ($counter%4 === 3) $link .= "</tr>";
$counter++;
}
Define a variable to count td's and check if it's rminder by 4 is 0 then open an close tr
$item = 0;
while ($row = mysql_fetch_array($q2)) {
if ($item % 4 == 0) $link .= "<tr>";
$link .= "<td>...</td>";
if ($item % 4 == 0) $link .= "</tr>";
$item++;
}
if($item % 4 != 0){//IF COUNT OF TD IN LAST ROW IS LESS THAN 4
while($item % 4 != 0){
$link.= "<td></td>";
$item++;
}
$link.= "</tr>";
}
So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
?>
<?php
$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);
echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row = $result->fetch_array()) {
$ass++;
echo '<tr>';
if ($ass == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($ass == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($ass == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($ass);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>
It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.
For example:
$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row = $result->fetch_array()) {
$ass++;
// check if value changes and reset rank if it does
if ($row['points'] !== $last_points) {
$rank = $ass;
$last_points = $row['points'];
}
echo '<tr>';
if ($rank == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($rank == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($rank == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($rank);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
So say your points looked like this:
100
100
90
80
The $rank value would be:
1
1
3
4
I have a page on my site that shows a member directory. I want the members to be listed 3 per row, before it goes to the next row. The code I have, does this for the very first row - but on the second row, its all on one line and doesnt carry down.
The profiles are showing up like this:
uuu
uuuuuuuuuuuuuuuuuuuuuuuuuuu
When they should be doing this:
uuu
uuu
uuu
uuu
This is what my code looks like:
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td></tr>
<tr>
<td>
<?php
while($row = mysql_fetch_array($result)) {
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td><td>';
if ($i++ == 2) echo '</td></tr><tr><td>';
}
?>
</td>
</tr>
</table>
Any help would be greatly appreciated, thanks!
Use
if (++$i % 3 == 0) echo '</td></tr><tr><td>';
Explanation:
First of all ++$i first increments $i, and then uses it in whatever is next, this makes for more readable code.
Second, the % is the modulus, which means it sortof subtracts 3 from $i until it is not possible anymore. E.g. 9 % 3 == 0, and 11 % 3 == 2 and so on. This means we know that we have printed 3 rows whenever $i % 3 equals 0.
try this one
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td>
</tr>
<?php
$count=0;
while($row = mysql_fetch_array($result))
{
$count+=1;
if($count%3==1)
{
echo '<tr>';
}
echo '<td>';
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td>';
echo '<td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td>';
if($count%3==0)
{
echo '</tr>';
}
}
if($count%3!=0)
{
echo '</tr>';
}
?>
</table>
You didn't reset the value of $i, so it kept increasing; another issue is that if you have only seven items, the last row should have four empty cells. So the loop condition needs to be augmented with a row completion status:
$i = 0;
while (($row = mysql_fetch_array($result)) !== false || $i != 0) {
if ($i == 0) {
echo '<tr>'; // start of new row
}
if ($row !== false) {
echo '<td>';
if (empty($row['profile'])) {
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'];
echo '</td>';
} else {
echo '<td></td><td></td>'; // no more data
}
$i = ($i + 1) % 3; // advance
if ($i == 0) {
echo '</tr>'; // end of the row
}
}
so im trying to create an image gallery that displays a random image in each cell in a 4 column table and automaticly extends as more images are added to the folder and trying to set it so that every time it load it randomizes the images. right it just reads the images in order and on each row it starts over instead of continuing on though the images.
my code:
$file = null;
$fileList = glob("./upload/*.*");
//create table tag
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
//create tr tag
echo '<tr>';
# Print each file
echo "Files found:"; foreach($fileList as $file) {
echo " - ". $file;
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>'; }
echo '</tr>';
echo '</table>';
that was my first try and it just create a single row
my second attempt:
//create table
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
echo '<tr>';
$x = 1;
$y = 1;
// Display the results
do {
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
$x = $x +1;
$y = $y +1;
}
while ($x <= 3);
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
echo '</tr>';
echo '<tr>';
$x = $x - 4;
$y = $y +1;
}
while ($x = 5);
}
while ($y <= 20);
echo '</tr>';
echo '</table>';
this time it just starts over on every row and create way to many rows
Your foreach loop starts over each time you call it. You should abandon the do/while loops and use for loops instead. One for the rows and one for the columns:
$fileList = glob("./upload/*.*");
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>'
// Determine max rows:
$max_rows = ceil(count($fileList) / 4);
// Keep an index
$index = 0;
// First loop for rows
for ($row = 0; $row < $max_rows; $row++) {
// Start a new table row
echo '<tr>';
// Second loop for columns
for ($col = 0; $col < 4; $col++) {
if ($index < count($fileList)) {
echo '<td width="25%"><img src="' . $fileList[$index++] . '" width="100%" /></td>';
}
else {
echo '<td></td>';
}
}
echo '</tr>';
}
echo '</table>';