i have the following code:
foreach($result->response->Results as $entry) {
echo '<tr class="'. $entry->EmailAddress.'">';
echo '<td>'. $entry->EmailAddress.'</td>';
echo '<td></td><td>';
foreach($unsubscribers->response->Results as $entry2) {
echo $entry2->EmailAddress; }
echo '</td><td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
the first loop pulls in a list of recipients email address's via the campaign monitor api, the second loop pulls in the people who have unsubscribed.
My problem is, there are 100 subscribers that get pulled in, and currently 1 of them have unsubscribed. That 1 unsubscriber gets looped through 100 times, and obviously gets displayed.
How would i go about adapting the above to make it so the unsubscriber doesn't show however many times there are subscribers.
Do you mean something like this?
// Add the unsubscribers to an array
$unsubs = array();
foreach($unsubscribers->response->Results as $entry2) {
$unsubs[] = $entry2->EmailAddress;
}
// Loop through the subscribers
foreach($result->response->Results as $entry) {
echo '<tr class="'. $entry->EmailAddress.'">';
echo '<td>'. $entry->EmailAddress.'</td>';
echo '<td></td><td>';
// If the subscriber is in our unsubscriber array, output the email again
if(in_array($entry->EmailAddress, $unsubs)) {
echo $entry->EmailAddress;
}
echo '</td><td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
It will only output the email in the second column if that subscriber is also in the unsubscribers array
check this codes its probably work for your way.
foreach($result->response->Results as $entry){
echo '<tr class="'. $entry->EmailAddress.'">';
echo '<td>'. $entry->EmailAddress.'</td>';
echo '</tr>';
}
foreach($unsubscribers->response->Results as $entry2){
echo '<tr class="'. $entry2->EmailAddress.'">';
echo '<td>'. $entry2->EmailAddress.'</td>';
echo '</tr>';
}
Related
I have the following table created with a foreach loop.
**foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
echo '<tr>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
echo '</table>';**
I want that the script checks inside of the table "y4qt2_jsn_users" of the mysql database and under a certain id number, if the "params" entry is "ja". If this is true then this certain table row should get another tr class.
My idea is something like that, but how can i combine this with my foreach table loop?
$result = mysqli_query("SELECT params FROM xxx_users WHERE params = 'ja' AND id= '$id'");
if(mysqli_num_rows($result) == 0) {
// row not found, just loop without a certain <tr class=""
} else {
// row found, give this row a <tr class=""
}
Here is a screenshot of what I want. If the mysql condition "params=ja" is true the whole row should get a new tr styleclass, not just a cell.
Screenshot
Screenshot-Database
Assuming I understand correctly, this should work. Please sanitize database queries (especially if there is user input at any point). But as mentioned in the comments, I don't know where $id is set, and actually presume it's a value in the $item array, so maybe replaced with $item['Id']
foreach($data2['wow_accounts']['0']['characters'] as $key => $item) {
$result = mysql_query("SELECT params FROM y4qt2_jsn_users WHERE params = 'ja' AND
id= '$id'");
if(mysql_num_rows($result) == 0) {
$class_string = '';
} else {
$class_string = ' class="my-additional-class"';
}
echo '<tr'.$class_string.'>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $item['realm']['name'];
echo '</td>';
echo '<td>';
echo '<button class="btnSelect">Select</button>';
echo '</td>';
echo '</tr>';
}
There are a lot of ways to clean this up, and if you wanted fewer lines of code you can replace the entire if else block and the $class_string variable by using ternary operators inline. The goal was to make it easy for you to read and simple to understand. (I also prefer very verbose code myself)
I am trying to post 3 variables via a Checkbox.
If a user ticks a Checkbox for each product, I am trying to send 3 variables:
<input type="checkbox" name="product[]" value="'. $rowAttr["reference"] .'-'. $rowAttr["price"] .'-'. $rowProd["unity"] .'">
The variables are definitely set as I have printed them all to the page where the user can see what Product they are selecting. However, when they submit the form, the variables from the checkbox are set to 1 characters? Where is this going wrong?
$selected = explode('-', $product);
$reference = $selected[0];
$price = $selected[1];
$unity = $selected[2];
echo '<table class="table table-striped">';
echo '<th>Product Reference</th><th>Price</th><th>Unit</th>';
if(isset($_POST['product'])) {
foreach($_POST['product'] as $product) {
print '<tr>';
print '<td>'. $product['reference'] .'</td>';
print '<td>'. $product['price'] .'</td>';
print '<td>'. $product['unity'] .'</td>';
print '</tr>';
}
}
echo '</table>';
try to run the code below
$product = $_POST['product'];
echo "<pre>";
var_dump($product);
echo "</pre>";
I think your array look like this
array(3){
[0]=>array(3) {[0]=>"some value",[1]=>"some value",[2]=>"some value",}
[1]=>array(3) {[0]=>"some value",[1]=>"some value",[2]=>"some value",}
[2]=>array(3) {[0]=>"some value",[1]=>"some value",[2]=>"some value",}
}
So do foreah or call $product[0][1], $product[0][2], $product[0][3]
instead.
At the moment I'm trying to understand the template view pattern using PHP.
I got a table that gets filled by a command (using command pattern). At the moment the array gets iterated and filled in a table using foreach. I want to split the program logic and the displaying of the table but I don't know where to declare the table and how to fill it.
I understood it generally. I'm able to display a single information.
Two questions:
How to implement it when I want to display a table from a database?
How to implement the content that should be displayed on all pages?
Unfortunately everything I found is about template engines that I don't want to use at the moment.
Here is the command combined with a table generator. I want to split that into a command that retrieves the data and a view that displays the table.
$domains = ConcreteDomainFactory::getAllDomains();
echo '<table class="domain-table">';
echo '<thead>';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Domain</th>';
echo '<th>Beschreibung</th>';
echo '<th>aktiviert</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
if((is_array($domains) ===true) && (count($domains) > 0)){
$line_number = 1;
foreach($domains as $domain){
echo '<tr>';
echo '<td>' . $domain->getPkDomainId() . '</td>';
echo '<td>' . $domain->getDomain() . '</td>';
echo '<td>' . $domain->getDescription() . '</td>';
echo '<td>';
if($domain->getActive()){
echo 'ja';
}
else{
echo 'nein';
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody>';
echo '</table>';`
<?php
echo '<table>';
for($i=1;$i<=12;$i++)
{
echo '<tr>';
while($row5=mysql_fetch_array($result5))
{
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
else if($row5[3]=='quarterly')
echo '<td rowspan="3">'.$row5[3].'</td>';
else if($row5[3]=='halfyearly')
echo '<td rowspan=""="6">'.$row5[3].'</td>';
else
echo '<td rowspan="12">'.$row5[3].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
This code is printing only one row instead of 12 rows. Please help me. I am doing this for managing student fees. I am stuck at the logic.
Create an array with sql result before :
$data = array();
while( $row5 = mysql_fetch_array($result5) )
$data[] = $row5;
Then replace this : while($row5=mysql_fetch_array($result5))
foreach ( $data as $row5 ) {
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
// ...
}
PS : Use mysqli_* instead of mysql_* which is deprecated
I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.