plus plus operator not working in php - php

<?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
}

Related

How do I loop through 2 columns?

I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>

Output first part of array differently than second part

<?php foreach ($rows as $id => $row): ?>
Basically i want my output for the first 5 rows to be inside <div class = "firstcolumn">
Once I have reached my five rows I want it to generate a second <div class = "secondcolumn"> for the rest of the rows. Also the row outputs have to come inside the div tags. So I only generate one div that fills up with rows
Can anyone help me to achieve this?
Try this
$cnt = 1;//Counter variable
foreach ($rows as $id => $row){
if($cnt <= 5){
$var = '<div class = "firstcolumn">';
}
else if($cnt > 5){
$var = '<div class = "secondcolumn">'
}
$cnt++;
}
In keeping with your style of coding, a loop counter will do the trick.
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<?php $colName = $i > 5 ? "secondcolumn" : "firstcolumn"; ?>
<div class="<?php echo $colName; ?>">
<?php endforeach; ?>
$count = 1;
foreach ($rows as $id => $row)
{
if($count <= 5)
{
if($count==1){
?>
<div class = "firstcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
else
{
if($count==6){
?>
<div class = "secondcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
if($count == 5){
?>
</div>
<?php
}
$count++;
}
if($count != 1 && $count!=6){
?>
</div>
<?php
}

print multiple arrays in foreach

How can i get row href to be 'id' string value from database ?
In other words, how can i show multiple arrays in one foreach ?
If theres any other logical errors in code you can point that out too.
<?php
$query = mysql_query("SELECT `id`, `url` FROM `bse` ORDER BY `id` DESC LIMIT 9");
while ($row = mysql_fetch_array ($query))
$post_id[] = $row['id'];
$rid[] = $row['url'];
}
$array = array(
"pid" => $post_id,
"vid" => $rid,
);
foreach (array_chunk($array["vid"], 3) as $rida) { ?>
<tr>
<?php foreach ($rida as $array["vid"])
{ ?>
<td><div class="a45"><a href="<?php echo $array["pid"]; ?>" title="nim" ><?php
echo $array["vid"];
?>
</a></div></td>
<?php } ?>
</tr>
<?php } ?>
</table>
You have two options. One is merging the arrays to just one array, take a read here:
how to join two multidimensional arrays in php
PHP combine two associative arrays into one array
The other (Thanks to Rafael!) is much easier.. Use something like this:
foreach($array as $i=>$v){
$other_array[$i];
}
You can use the $i and look in the other array. Credits ofcourse to Rafael.
Is this what you're looking to do?
<?php
$query = mysql_query("SELECT `id`, `url` FROM `bse` ORDER BY `id` DESC LIMIT 9");
if($query){
?>
<table>
<?PHP
while ($row = mysql_fetch_array ($query)){
?>
<tr>
<td>
<div class="a45">
<a href="<?php echo $row['id']; ?>" title="nim" >
<?php echo $row['url']; ?>
</a>
</div>
</td>
</tr>
<?php
}
?>
</table>
<?PHP
} else {
echo "No results";
}
?>
If not, what is it that you're trying to accomplish?

PHP Increment number, each time a new div is created

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;

Alternating CSS Style with multiple Mysql Results

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

Categories