Basically I have a PHP script that creates a div for each item in the database, but I want to have a "rank" number in each div created, i.e:
-----------------
Rank: 1 < div 1
-----------------
Rank: 2 < div 2
-----------------
Rank: 3 < div 3
-----------------
And so on..
Here's my current code...
while($row = mysql_fetch_array($result)) {
$name = stripslashes($row['name']);
$description = stripslashes($row['description']);
$votes = stripslashes($row['votes']);
$id = ($row['id']);
$link = ($row['link']);
$rank = 0;
?>
<div class="site" id="site">
<u><center>
<strong><?php echo $name; ?></strong></u>
</font></center>
<p>Rank:<?php echo $rank++ ; ?></p>
<p><b><?php echo $description; ?></b><br />
Votes:<b> <?php echo $votes; ?></b><br />
</p>
</div>
<center>
<?php
}
?>
But that doesn't work, any help would be greatly appreciated. (Also, the div's continue over multiple pages).
Put the $rank = 0; outside of the loop. Otherwise it will be always 0.
You define the $rank-variable in your loop, so in every loop-round it's defined with the value 0. Define the counter-variable outside the loop and increase it in the loop.
$rank needs to be defined outside of the while statement. Every time it loops it's resetting to zero. Also, increment $rank elsewhere - it makes the code slightly more robust and intelligible.
Rank need to be outside the loop or you will reset it to 0 everytime
$rank = 0;
while($row = mysql_fetch_array($result)) {
$name = stripslashes($row['name']);
$description = stripslashes($row['description']);
$votes = stripslashes($row['votes']);
$id = ($row['id']);
$link = ($row['link']);
?>
<div class="site" id="site">
<u><center>
<strong><?php echo $name; ?></strong></u>
</font></center>
<p>Rank:<?php echo $rank ; ?></p>
<p><b><?php echo $description; ?></b><br />
Votes:<b> <?php echo $votes; ?></b><br />
</p>
</div>
<center>
<?php
$rank++;
}
?>
Additional for comment:
Use an offset for that, for example
$result_per_page = 5; // this is the number of result you show per page
$offset = isset($_GET['id'])? (int)$_GET['id'] : 1;
$rank = $offset * $result_per_page;
Related
<?php
$qry = mysql_query("select branch from branches");
while($row = mysql_fetch_array($qry)) {
$branch = $row['branch'];
$i=1;
?>
<div class="dropdown">
<input type="checkbox" id="drop<?php echo $i; ?>" />
<label for="drop<?php echo $i++; ?>" class="dropdown_button"><?php echo "$branch"; ?><span class="arrow"></span></label>
<ul class="dropdown_content">
<?php
$qry1 = mysql_query("select sub_dir from `$branch`");
while($row1 = mysql_fetch_array($qry1)) {
$sub_topic = $row1['sub_dir'];
?>
<li><?php echo "$sub_topic"; ?></li>
<?php
}
?>
</ul>
</div>
<?php }?>
In line number 9, you can see, inside 'label' tag, I am trying to increment the variable '$i' but of no use because at every call to next row from the table, the value of '$i' remains same i.e. '1'. Where I am doing wrong.
Another question: Is it possible to create id's at run time as I did in line 8, i.e 'drop1, drop2,...'. May be its foolish way of doing something but I am trying new things.
$i initialization will be before the while loop
$i=1;
while($row = mysql_fetch_array($qry)) {
$branch = $row['branch'];
Initialize $i before the loop begin
$i=1;
while($row = mysql_fetch_array($qry)) {
//while loop block
}
I have a query that selects 3 rows; but I only want to display the one row at a time, use the data from that row, then move to the next row, then repeat. How would this be accomplished? Table has 4 items in it.
PHP:
<?php
$server = "secret";
$user = "secret";
$pass = "secret";
$db = "secret";
$con=mysqli_connect($server,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';
mysqli_select_db($con, $db);
$query = mysqli_query($con, $sql);
$number = 1;
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
mysqli_close($con);
?>
Attempt to display:
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
?>
Output of attempt:
Test
||
7/14/13
News 1
Testing to see if this will work lalalalalla
NOTE: Tried to repeat the attempt to see if it would work, but it displayed same as output but with a second one that was a duplicate except said News 2
Desired output look:
Newest Title | Newest Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.
2nd Newest Title | 2nd Newest Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.
3rd Newest Title | 3rd Newest Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.
The problem is that you are overwriting your variables with each while() loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
}
so when you try to echo $id, $title, $news_date, and $post, you will only get the last row data
you either need to add your html in the while loop -
while ($result = mysqli_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
?>
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
}
or save them to an array
$id = array();
$title = array();
$news_date = array();
$post = array();
while ($result = mysqli_fetch_array($query)) {
$id[] = $result['id'];
$title[] = $result['title'];
$news_date[] = $result['news_date'];
$post[] = $result['post'];
}
and then access the array in your display
<div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post[$number-1]; ?></p>
<?php
$number++;
?>
I used [$number-1] as I assume $number is starting at 1, and arrays are 0 based
edit here are ways to echo your html. It would be beneficial to visit the php documentation, as all of this is there. In these I set $number back to 0, but you can change it back to 1, and just change each of the $number->$number-1, and the $number+1->$number
using a for loop - http://php.net/manual/en/control-structures.for.php
<?php
for($number=0;$number<count($title);$number++){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a foreach loop - http://php.net/manual/en/control-structures.foreach.php
<?php
foreach($title as $number => $value{
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
}
?>
using a while loop - http://php.net/manual/en/control-structures.while.php
<?php
$number = 0;
while($number<count($title){
?>
<div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
<p>News <?php echo $number+1; ?></p>
<p><?php echo $post[$number]; ?></p>
<?php
$number++;
}
?>
each of the above was done by closing out of php, and writing the html, and then opening php again. You could also just stay in php, and echo the html - http://php.net/manual/en/function.echo.php
<?php
for($number=0;$number<count($title);$number++){
echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";
echo "<p>News ".($number+1)."</p>";
echo "<p>".$post[$number]."</p>";
}
?>
This is a very basic problem but I am very new to PHP.
I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble :(
Right now, I am just using the simple way i.e.
while($data = mysql_fetch_array($res_set)) {
?>
<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
this is my code
$cnt = 0;
while ($prd = mysql_fetch_array($res)) {
?>
<div class="imageRow">
<?php
$cat_id = $prd['cat_id'];
$sql = "select * from tbl_category where id = $cat_id";
$cat_res = mysql_query($sql);
$cat_data = mysql_fetch_array($cat_res);
$cat_name = $cat_data['name'];
?>
<div class="set">
<div class="single first">
<img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /><br />
Choose
</div>
</div>
</div>
<?php
$cnt = $cnt++;
if($cnt%3 == 0) {echo "<br />";}
}
//echo $cnt;
?>
any help will be appreciated.
Thanks
<div class="imageRow">
<?php
$i = 0;
while($data = mysql_fetch_array($res_set)) { ?>
<div>
<?php echo $data['name']; ?><img src="images/<?php echo $data['image']; ?>" />
</div>
<?php
$i++;
if($i % 3 == 0) {
echo '</div><div class="imageRow"><br />';
}
}
?>
</div>
Explanation:
Set the variable $i to a number, which will then be used to keep track of how many items you've written.
Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you're on - whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end - other like to set it to 0, and set it near the beginning - either is completely fine - whatever you need it to do / whichever way you like better.
Lastly, check if $i is evenly divisible by 3 (kind of like remainder - it's called "mod" and is represented by the percent symbol). If it is, then echo a line break.
Try this
<?php
echo "<div>";
while($data = mysql_fetch_attay($res_set)) {
?>
<div style="width:30%; float:left"><?php echo $data['name]?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
echo "</div>";
?>
Output
<div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
</div>
That's some super ugly code you have there. This will do what you need, and is cleaner.
<?php
$i=1;
while($data = mysql_fetch_attay($res_set)) {
$frame = '<div><img src="%s" /></div>';
printf($frame, $data['image']);
if($i % 3 == 0) { echo '<br />'; }
$i++;
}
Also, <img> elements are already block elements to begin with, you really don't need to wrap another <div> around them.
I have downloaded the pagination code and used it for my website running on my local machine. After modifying and testing, there was a problem where it does not recognize the variable $page.
Please refer to the following code:
<?php
$rpp = 3; // results per page
$adjacents = 4;
$page = intval($_GET["page"]);
if($page<=0) $page = 1;
$reload = $_SERVER['PHP_SELF'];
$sql = "SELECT * FROM ".TABLE_IMAGE." ORDER BY id ASC";
$qry = mysql_query($sql, $con);
// count total number of appropriate listings:
$tcount = mysql_num_rows($qry);
// count number of pages:
$tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number
$i = 1;
$count = 0;
$j = ($page-1)*$rpp;
while(($result = mysql_fetch_array($qry)) && (($count<$rpp) && ($j<$tcount))){
$id = $result['id'];
$img = $result['path'];
$title = $result['title'];
$detail = $result['detail'];
$priority = $result['priority'];
$active = $result['isActive'];
?>
<div id="block-image-slider" class="<?php echo(($i%2==0)?'even':'odd')?>">
<h2><?php echo $title ?></h2><span class="operation">[កែប្រែ|លុប]</span>
<div class="block-slider-image-body">
<div class="left">
<ul>
<li>លេខរៀងទី<span class="space"><?php echo $id ?></span></li>
<li>កំនត់អទិភាពទី<span class="space"><?php echo $priority ?></span></li>
<li>ត្រូវបានបង្ហាញអោយឃើញ<span class="space"><?php echo (($active==1)?'បង្ហាញ':'មិនបង្ហាញ')?></span></li>
<li>អត្ថបទពេញ<div class="detail"><?php echo $detail ?></div></li>
</ul>
</div>
<div class="right">
<img src="<?php echo '../../image/Slider/'.$img ?>" alt="<?php echo $title ?>" width="170" height="100" />
</div>
<div style="clear:both;"></div>
</div>
</div>
<?php
mysql_data_seek($qry,$j);
$i++;
$j++;
$count++;
}//end of while loop
include("../include/paginate.php");
echo paginate_three($reload, $page, $tpages, $adjacents);
?>
This is the error it generated:
Notice: Undefined index: page in C:\wamp\www\1. Chirst Joy Church Website\common\admin\index.php on line 87
How can I fix this error?
This won't work unless your URL ends with ?page=3 (where 3 would be the current page number). In order to fix it you should change this line
$page = intval($_GET["page"]);
to
$page = (!empty($_GET["page"]) ? intval($_GET["page"]) : 1);
I've got a site where someone searches for x product in their location and the site spits back a list of results.
if(isset($_POST['zip'])){
$qry="SELECT business_id FROM ".TBL_BUSINESS." WHERE zip LIKE '%".$_POST['zip']."%'";
$rs = mysql_query($qry);
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE ){
$rec[] = $row[0];
}
if(!empty($rec[0])){
echo "Products for this location<br/>";
foreach ($rec as $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="norm">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
}
else
{
echo "No Product is added for this location";
}
}
?>
What would be the best way to alternate <div class="norm"> with <div class="alt">?
keep a counter and use it's value modulo 2 to determine whether the class should be "norm" or "alt".
$rec2 = mysql_fetch_array($rs2);
$count++;
?>
<div class="<?php echo($count%2?"norm":"alt"); ?>">
I tend to use something like this:
$row = 0;
foreach ($rec as $result) {
$class = $row++ & 1 == 1 ? 'alt' : 'norm';
...
echo <<<END
<div class="$class">
...
END;
}
You can use curly braces to do the expression within the string but I generally prefer not to embed that kind of logic. It's (imho) a little harder to read. Plus the above gives you the opportunity to output it for debugging purposes more easily, etc.
if(some expression)
{
$class="norm";
}
else
{
$class="alt";
}
?>
<div class="<?php echo $class;?>">
set a counter on your output loop. Whenever the counter is even, set the class to normal, else set it to alternate.
Why not use modulus and a row id? Much simpler, no need for unnecessary variables
foreach ($rec as $rid => $result)
{
$bid = $result;
$qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
$rs2 = mysql_query($qry2);
$rec2 = mysql_fetch_array($rs2);
?>
<div class="<?=($rid % 2 == 0) ? 'norm' : 'alt' ?>">
<img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
<h3><?echo $rec2['name'];?> <?php echo $rec2['prodvalue']?></h3>
<div class="prodlistMeta">
<a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
<a class="print" href="#">Print</a>
</div>
</div>
<?php
}
Widespread adoption of CSS 3 can't come soon enough: http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#structural-pseudos