Display Only Values not retrieved by array - php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<select>
<?php
include 'connection.php';
$q = "SELECT * FROM rooms WHERE duration='first lecture' and day='Sunday'";
$r = mysql_query($q);
$ro = mysql_num_rows($r);
while($row = mysql_fetch_array($r)){
for ($i=1; $i<=14; $i++)
{
$exclude = array($row['name']);
if(in_array($i, $exclude)) continue;
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
}
?>
</select>
</body>
</html>
I want to use a FOR LOOP to eliminate elements retrieved by an array in one loop. Suppose $row retrieved some values , what I want with that FOR LOOP is to display numbers between 1 to 14 except the retrieved values .
I tried it many times and I succeed in that, but the loop in first time eliminate just first retrieved and in second time eliminate just second retrieved value.
Is there's a way to eliminate both at one time ?

Considering $row['name'] is a int and use the below code
while($row = mysql_fetch_array($r)){
$exclude[] = $row['name'];
}
for ($i=1; $i<=14; $i++)
{
if(in_array($i, $exclude)) continue;
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
Start using mysqli_* function. mysql_* functions are depreciated.

First off you should be using PHP PDO for db interactions, mysql_fetch_array has been deprecated. Here is a link to the PDO's manual http://php.net/manual/en/book.pdo.php
See answer bellow:
<?php
$q = "SELECT * FROM rooms WHERE duration='first lecture' and day='Sunday'";
$stmt = $db_pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<select name="" id="">
<?php
for ($i = 1; $i <= 14; $i++):?>
<?php $good_to_go = true; ?>
<?php foreach ($data as $exclude): ?>
<?php if ($exclude['name'] == $i): ?>
<?php $good_to_go = false;?>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($good_to_go): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endif; ?>
<?php endfor; ?>
</select>

Related

Echo in for loop

This is my code:
<?php
$date = 2015-02-30;
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
?>
<select>
<?php
for ($i=1; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php if($day === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php } ?>
</select>
For the option, the number 4 should be selected. Why doesn't it work? Thanks
Sorry, I already had this in a select statement
EDIT: See the code edit above. Maybe because the
You need to wrap your code in a select statement!
An option statement wont work without the select tag around it:
<html>
<body>
<select> <!-- Start the select statement -->
<!-- Your Code -->
<?php
$num = 4;
for ($i=1; $i < 10; $i++)
{
?>
<option value="<?php echo $i; ?>" <?php if($num === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php
}
?>
<!-- End your code -->
</select> <!-- End the select statement -->
</body>
</html>
After your edit, it looks like this is your main problem:
$date = 2015-02-30;
This is not what you think it is. It should be quoted like this:
$date = '2015-02-30';
Otherwise, $date is a not a string, it's a math expression that evaluates to (int) 1983, so substr($date, 8, 2); will evaluate to false, not 30, and then obviously your option won't be selected.

PHP Codeigniter - How to set value to create database by option select

I try to make a shipping method using Rajaongkir API, but I need to add some data to my database that contain the shipping cost <?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value'] ?>
but, it is always show an"0" in my database
this is my view
<?php echo form_open('orders/proceed'); ?>
<select class="form-control">
<option name="ongkir" selected="" disabled="">Pilih Layanan</option>
<?php
for ($l=0; $l < count($data['rajaongkir']['results'][$k]['costs']); $l++) {
?>
<?php echo "<option value='".$data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value']."'>" ?>
Layanan <?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['service'] ?> |
<?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['etd'] ?> Hari | Rp.
<?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value'] ?>
<?php "</option>"; ?>
<?php
}
?>
</select>
and this is the controller
$carts = $this->cart->contents();
$ongkir = $this->input->post('ongkir');
foreach ($carts as $item) {
$product = $this->Product->getProductByCode($item['id']);
$detail['code'] = $item['id'];
$detail['name'] = $item['name'];
$detail['price'] = $item['price'];
$detail['qty'] = $item['qty'];
$detail['discount_percent'] = $item['options']['discount_percent'];
$detail['net_price'] = $product['net_price'];
$detail['weight'] = $item['options']['weight'];
$detail['order_id'] = $orderId;
$detail['ongkir'] = $ongkir;
$this->Order_detail->create($detail);
}
this is the screenshot
I cannot add the shipping cost to my value.
How to resolve it and and those shipping cost value to my database?

PHP foreach: data as value less than a number

I need the code to be executed only if value of $entry[0] is less than 10, but it doesn't work.
I know the error is here: $data as $entry[0] < 10
The data is an excel worksheet and $entry[0] is serial numbers from 1 to 100 and $entry[1] is text field (domain names).
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry[0] < 10): ?>
<?php echo $entry[0]." ";?>
<a href ="<?php echo "http://". $entry[1]; ?>" target="_blank">
<?php echo $entry[1];?></a><br />
<hr />
<?php endforeach; ?>
You are using foreach in a wrong way. First, iterate over data, then apply your logic in your sheet row data:
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry): ?>
<?php if($entry[0] < 10): ?>
<?php echo $entry[0]." ";?><?php echo $entry[1];?><br />
<?php endif; ?>
<hr />
<?php endforeach; ?>
<?php
$data = wp_excel_cms_get("top100");
foreach($data as $entry[0] ){
if ($entry[0] < 10){ echo $entry[0]." ";?>
<?php echo $entry[1];?><br />
<?php } ?>
<hr>
<?php endforeach; ?>

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
}

Categories