print multiple arrays in foreach - php

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?

Related

plus plus operator not working in 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
}

MySQL query doesn't show all the records correctly

The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.

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

PHP MYSQL Multiple table query

I have two tables 'p_tuts' and 'h_tuts'. I'm using a method to display all the returned rows. Although I'm not sure how to set it up to run multiple queries, I can only get it to return one query at a time. Heres my code...
public function QueryAll($my_field, $limit) {
$query = mysql_query("SELECT * from $my_field LIMIT $limit");
$rows = array();
while($row = mysql_fetch_array($query)) {
$rows[] = $row;
}
return $rows; }
Here is my index file
<?php $results = $Database->QueryAll('p_tuts', 5); ?>
<?php foreach ($results as $result): ?>
<div class="tutorial">
<div class="tut_header"><h2><?php echo $result['tut_header']; ?></h2></div>
<div class="tut_poster">Posted by: <?php echo $result['tut_poster']; ?> on <?php echo $result['tut_date']; ?></div>
<div class="tut_img rounded"><img src="<?php echo "img_uploads/". $result['tut_img']; ?>" width="180" height="151" /></div>
<div class="tut_content"><p><?php $Website->CharLimit($result['tut_content'], 800); ?></p></div>
</div>
<?php endforeach; ?>
I'd really like to adapt it so I can use this class to display both multiple tables, rather than just the one.
kind regards
This is difficult to answer without knowing the relationship between the two tables, but it looks like you need to use a JOIN or UNION.

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