How to add hyperlink on array values in a table? - php

I'm wanna to add hyperlink on every emails and phone numbers that appear in the table like shown below
This is the code that I'm writing right now.
<tbody>
<?php
$grand_total_amount = 0;
foreach ($processed_sales as $key => $sale):
?>
<tr class="sale" data-id="<?= $sale['id']; ?>">
<td><?= $pagination->offset + $key + 1; ?></td>
<?php
foreach ($columns as $key => $value):
if ($checked_columns[$key]):
?>
<td><?= $sale[$key]; ?>
<?php echo $value['email']; ?></td>
<?php
endif;
endforeach;
?>
</tr>
<?php
$grand_total_amount = bcadd($grand_total_amount, $sale['total_amount'], 2);
endforeach;
?>
</tbody>
I only want the data in column email and phone number to have hyperlink that will direct them to other pages.

It's quite unclear what you're doing but what I could figure out is, your <a> tags should be within <td> tags, like this:
<td>
<a href="../Jualan.php?email=<?php echo $value['email']; ?>" class="style1">
<?php echo $value['email']; ?>
</a>
</td>
Otherwise it would messup the table.

Your should be inside your maybe that's why it doesnt works
echo "$value['email'];";

Related

Mysql returns duplicate in html table

I am trying to populate html table with data from mysql.But i am stuck on this part where each time i add some data it keeps repeating. On the picture below you can see that Test 1 repeat each time for every P20,P21,P24,P22,P23 and i needed to be one TEST1 for all of them. When i add Test 2 with value 19000 its making new P20 and all data come from Test 1 to Test 2. Can someone help me how to fix this.Any hint or suggestion wil be appreciated. Thank you all very much (Sorry for my bad english)
This is code that runs this
$sql = 'SELECT DISTINCT Pers.naam, Rol.funkcija,pdata.broj
FROM ids
left JOIN Pers ON ids.persid = Pers.id
left JOIN Rol ON ids.rolid = Rol.id
left JOIN pdata ON ids.pdataid = pdata.id
';
$query = $conn->prepare($sql);
$query->execute();
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php
foreach ($testing as $test):
?>
<th>
<?php
echo $test['naam'] . '<br />';
?>
</th>
<?php
endforeach;
?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($testing as $test): ?>
<tr>
<td><?php echo $test['funkcija']; ?></td>
<?php endforeach; ?>
<?php
foreach ($testing as $test):
?>
<td><?php echo $test['broj']; ?></td>
<?php
endforeach;
?>
</tr>
</table>
I would like to have it like this
There is some refactoring required to get to the output you want.
You seem to be enumerating quite a lot of data from your SQL result, so there is some grouping required to make things easier.
To get the appropriate number to each "naam" and "funkcija" you can use array_filter which however could theoretically return several numbers in each case.
You'll also have to nest a couple foreach instead of running them after one another.
If I understand your data structure correctly, this should at least give you a good starting point for the output you want:
<?php
// ...
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
$naams = array_unique(array_column($testing, "naam"));
$funkcijas = array_unique(array_column($testing, "funkcija"));
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php foreach ($naams as $naam): ?>
<th>
<?php echo $naam; ?>
</th>
<?php endforeach; ?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($funkcijas as $funkcija): ?>
<tr>
<td><?php echo $funkcija; ?></td>
<?php foreach ($naams as $naam): ?>
<?php
$data = array_filter(
$testing,
function ($v) use ($naam, $funkcija)
{
return $v["naam"] === $naam && $v["funkcija"] === $funkcija;
}
);
foreach ($data as $value): ?>
<td><?php echo $value["broj"]; ?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

Foreach display only first result

How can I make this foreach line only go through the first result?
<?php foreach ($collection as $product) : ?>
I tried the following, but that does not work. It than does not display any results:
<?php foreach (array_slice($collection, 0, 1) as $product) : ?>
EDIT:
The following works perfect:
<?php
$i = 0;
foreach ($collection as $product) :
if($i < 1) {
Inner content
}
$i++;
endforeach;
?>
Total current code:
<tbody>
<tr>
<td class="image" rowspan="3">
<?php $i = 0; foreach ($collection as $product) : if($i < 1) {?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75) ?>" alt="<?php echo $this->htmlEscape($product->getName()) ?>" width="75" height="75" />
<?php } $i++; endforeach ?>
</td>
<td class="order" colspan="2">order</td>
<td class="exclTax">excl. vat</td>
<td class="inclTax">incl. vat</td>
</tr>
<?php foreach ($collection as $product) : ?>
<tr>
<td class="select"><input type="radio" name="featured_1807" id="featured_1807_1349567899" value="3071895, IM" data-product-id="3071895" data-product-sup="IM"></td>
<td class="title"><?php echo $this->htmlEscape($product->getName()) ?></td>
<td class="price"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
<td class="priceIncl"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
</tr>
<?php endforeach ?>
</tbody>
How can I achieve that?
Why bother with a loop if you are only interested in the first occurance of $collection
You could do this instead and probably not have to change any code you currently have inside your loop
<?php
$product = $collection[0];
Code you had in the loop
?>
If its not a numeric key then you can use
<?php
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
... html stuff
// and then do again for your second loop
// if you only want the first occ of that as well
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
reset($collection); // Resets the array's internal pointer to the first element
$product = current($collection); // Returns the current element which is now first element
As far as I'm concerned, this is the best solution I would ever go for if I need to only extract the first element from an array.

iterating in two arrays in the same for loop

I'm learning php and I want to know how to iterate in two arrays in the same for loop? This is the code I have, line 8 to line 14 for example, but it gives an array instead of an element, what to do? please be easy on me I'm still new to this field.
<tr>
<td><?php echo $val->title ?></td>
<td><?php echo $val->content?></td>
<td><?php echo $val->owner ?></td>
<?php } ?>
<?php
foreach ($users as $val) {
?>
<td><?php echo str_replace("-", "%", $val->email) ?></td>
<?php }?>
<?php foreach ($maincat as $val) {?>
<td><?php echo str_replace("-","%",$val->maincat_name) ?></td>
<?php }?>
<?php foreach ($category as $val) {?>
<td><?php echo str_replace("-","%",$val->category_name) ?></td>
<?php }?>
<?php foreach ($sub_category as $val) {?>
<td><?php echo str_replace("-","%",$val->sub_category_name) ?></td>
<?php }?>
<?php foreach ($sub_sub_category as $val) {?>
<td><?php echo str_replace("-","%",$val->sub_sub_category_name) ?></td>
<?php }?>
</tr>
In this image, it gives the whole cells
instead of the two cells {sajeda#sajeda.com and email2} , I want the cell sajeda#sajeda.com to be in the bottom and the other one *email*2 to be above.
This is not an html question, when iterating, I want the first value to be sajeda#sajdea.com and the other iteration to give email2
can somebody help ?
EDIT :
this is how i define users
$data['users'] = $this->model->join_users($table)->result();
I don't know is this what you're looking for?
I used a couple shorthands such as <?=
to replace <?php echo and using foreach(...) : endforeach;
<?php $i = 0; ?>
<?php foreach ($users as $val) : ?>
<td><?= str_replace("-", "%", $val->email) ?></td>
<td><?= str_replace("-", "%", $maincat[$i]->maincat_name) ?></td>
<td><?= str_replace("-", "%", $category[$i]->category_name) ?></td>
<td><?= str_replace("-", "%", $sub_category[$i]->sub_category_name) ?></td>
<td><?= str_replace("-", "%", $sub_sub_category[$i]->sub_sub_category_name) ?></td>
<?php $i++; ?>
<?php endforeach; ?>
result() as $msg):?>
db->select("lastname, firstname,mi")->from("tb_employee")->where("employee_id",$msg->sender)->get(); ?>
<a href=<?php echo base_url() ."mainx/show_msg_id/". $msg->msg_id; ?> >
<?php foreach($s->result() as $t):?>
<div>
<strong><?php echo $t->lastname . ' ' . $t->firstname;?></strong>
<span class="pull-right text-muted">
<em><?php echo date('m/d/Y H:i:s' ,$msg->timestamp); ?></em>
</span>
</div>
<div><?php echo $msg->message;?></div>
</a>
<?php endforeach; ?>
<?php endforeach; ?>
What i do in my example is that i am loop to foreach. On my first loop is the name of employee and in between of my employee loop is the message foreach loop.

For loop inside Foreach

I got a foreach which loops through my database for each user.
<?php
foreach($users as $user) {
?>
<tr>
<td>
<?php
for ($i=1; $i < 52; $i++) {
echo $i;
}
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
This loop through database and echos the username, now I tried to build a for loop inside this so that every user has a number, I took as an example a very basic for loop ( it will be changed later).
The problem is that I get all numbers printed out for each user, but I just want to print out the number once for a user. How do I solve this.
If you want unique index number for user, you do not need extra loop.
Just add an increment variable:
And increment it in existing loop.
<?php
$i=0;
foreach($users as $user) {
++$i; // For printing first user, it will be 1 not 0.
// Therefore, increment is placed before printing name.
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php
}
?>
This should be enough to achieve what you're trying to do :
Your for() isn't needed since foreach() already create a loop, you just have to use this loop to increment a value (here called $i) then display it.
Also you should avoid to open your php tags ten thousands times for a better visibility into your code :)
<?php
$i = 0;
foreach($users as $user) {
$i++;
echo'<tr>
<td>'.$i.'</td>
<td>'.$user['firstname'].'</td>
</tr>';
?>
<?php
$i=1;
foreach($users as $user) {
?>
<tr>
<td>
<?php
echo $i++;
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
Try to use the key in foreach
<?php foreach ($users as $key => $user) : ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php endforeach; ?>

PHP table from JSON without names in array

I am having a problem with api Yandex.
I have to get data from api and make with php the table:
Source Users New Pages Bounce Goal1 Goal2
organic s1v1 s1v2 s1v3 s1v4 s1v5 s1v6
referral s2v1 s2v2 s2v3 s2v4 s2v5 s2v6
(none) s3v1 s3v2 s3v3 s3v4 s3v5 s3v6
After json_decode I have:
{"data":[
{"dimensions":[{"name":"organic"}],"metrics":[s1v1,s1v2,s1v3,s1v4,s1v5,s1v6]},
{"dimensions":[{"name":"referral"}],"metrics":[s2v1,s2v2,s2v3,s2v4,s2v5,s2v6]},
{"dimensions":[{"name":"(none)"}],"metrics":[s3v1,s3v2,s3v3,s3v4,s3v5,s3v6]},
]}
But I can't correctly parse it into a table. So far I have written code only for the first column Source, and then stuck:
<?php
$metrika_o = json_decode($metrika);
echo "<table>
<tr>
<td><strong>Source</strong></td>
<td><strong>Users</strong></td>
<td><strong>New</strong></td>
<td><strong>Pages</strong></td>
<td><strong>Bounce</strong></td>
<td><strong>Goal1</strong></td>
<td><strong>Goal2</strong></td>
</tr>";
foreach($metrika_o->data as $data)
foreach($data->dimensions as $source)
:
?>
<tr>
<td><?php echo $source->name?></td>
</tr>
<?php endforeach;
echo "</table>";
?>
The Source number is constantly changing, the set of columns is fixed.
Please help me to solve this task
You are on the right way:
<?php foreach($metrika_o->data as $data): ?>
<tr>
<td><?php echo $data->dimensions[0]->name; ?></td>
<?php foreach(explode(',',$data->metrics) as $col):
<td><?php echo $col; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

Categories