Return Unique Value from Chunked Array - php

I have a calendar script that outputs to a text file. I'm opening the text file, reading it into an array and then outputting the results. The text file contains:
7/9/2013-7/13/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
7/16/2013-7/20/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
My PHP code looks like this:
$content = file('DC_PictureCalendar/admin/database/cal2data.txt');
$content_chunked = array_chunk($content, 6);
if (count($content_chunked > 0))
{
echo "<table>";
for ($i=0;$i<count($content_chunked);$i++)
{
echo "<tr>";
echo "<td valign='top'>";
echo "<div style='padding-top:6px;'>";
echo "<a href='schedule.php'>";
echo "<img src='DC_PictureCalendar/admin/database/images/".$content_chunked[$i][3]."' width='80' height='80' border='2'>";
echo "</a>";
echo "</div>";
echo "</td>";
echo "<td valign='top'>";
echo "<div style='padding-left:5px;'>";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<h2>";
echo "<a href='schedule.php'>";
echo $content_chunked[$i][1];
echo "</a>";
echo "</h2>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo $content_chunked[$i][2];
echo "<a class='green' href='schedule.php'>";
echo "Read more..";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
Problem is, if there is a duplicate entry in the $content_chunked[$i][1] (which is the title in this case), I just want to display it once instead of twice. Is this possible? I thought array_unique might work but it didn't seem to help. Thanks in advance!

array_unique() RETURNS the array without duplicates, but does NOT modify the original array!
so:
$a = [1, 1, 2, 3]
array_unique($a) => [1, 2, 3]
$a => [1, 1, 2, 3]
The new array needs to be saved in a variable for you to access it later.
$a = [1, 1, 2, 3]
$b = array_unique($a)
$b => [1, 2, 3]

Although its possibly not the most graceful / efficient.. Add this in in the relevant place:
echo "<table>";
$titles = array();
for ($i=0;$i<count($content_chunked);$i++)
{
if (in_array($content_chunked[$i][1], $titles)) continue;
$titles[] = $content_chunked[$i][1]

Related

How can i get the quantity of every product outside of my while loop

I am fetching data from the database, the id, name and price. But inside the while loop is also a quantity. I dont know how to catch the quantity of every product. The quantity repeats with the new product inside the while loop. But if i want to catch the quantity of all products outside the while loop i only get one value. I hope someone can give me a answer with a post (i am a beginner).
Here comes the code
$arr = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4>
</div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>
{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "
</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
}
You are halfway there. You are incrementing inside loop but to get total you need to print out outside loop. Also you need to declare variable total at the beginning.
Try this :
$arr = array();
$total = 0; //declare variable
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4>
</div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>
{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "
</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;//increment inside loop
}
echo $total;//print outside loop

PHP Associative Array To Table

I am trying to sort an associative array in ascending in order and then transfer it to a HTML table and I am currently stumped at an error. I looked for guidelines here on SO and followed instructions on some posts:
PHP display associative array in HTML table
But still no luck, here is my attempt:
<?php
function format($g){
array_multisort($g, SORT_ASC);
echo "<table>";
foreach($g as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
My debugger is telling me there is an error at my for each loop but I don't see how it is wrong unless there is some syntax error that I am not seeing? The error is saying Invalid argument supplied for foreach()
Because your $bib is only single array but you use two foreach to loop this array
At 2nd loop, your $row variable is a string, you can't use foreach for this type
Can you try that for single array ?
<?php
function format($data) {
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $k => $v) {
echo "<tr>";
echo "<td>$k</td>";
echo "<td>$v</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
$k is Luke John Matt and Mark,
$v is 10 30 20 and 40
You can see the foreach example here: http://php.net/manual/en/control-structures.foreach.php
Hope this helpful ^^
You can try this
<?php
function format($data){
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $key => $row) {
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>" . $row . "</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array(
"Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40"
);
format($bib);
?>

Create hyperlink from html table row to open record on a new page

To begin with I know very little PHP and no java/javascript or jquery. I have created an html table populated from mysql database. It is for a call log. I am wanting to click on either a <td> or <tr> to open the corresponding record in a new page to be viewed in more detail. I have thought about putting the call_id value in a hidden column to be used in a variable somehow, but don't know where to go from there or if that is anywhere near the correct way to accomplish this.
Assuming that you're creating the table doing something like this,
$results = some_mysql_query;
foreach ($results as $index => $array) {
echo "<tr>";
echo "<td>";
echo $array['RecordNumber'];
echo "</td>";
echo "<td>";
echo $array['CallerName'];
echo "</td>";
echo "<td>";
echo $array['Time'];
echo "</td>";
echo "<td>";
echo $array['Duration'];
echo "</td>";
echo "</tr>";
}
Then you can add in a link by doing something like this:
$results = some_mysql_query;
foreach ($results as $index => $array) {
echo "<tr>";
echo "<td>";
echo "<a href='recordInfo.php?record='" . $array['RecordNumber'] . "'>" . $array['RecordNumber'] . "</a>";
echo "</td>";
echo "<td>";
echo "$array['CallerName'];
echo "</td>";
echo "<td>";
echo $array['Time'];
echo "</td>";
echo "<td>";
echo $array['Duration'];
echo "</td>";
echo "</tr>";
}
NOTE the use of single-quotes, ', inside the double-quotes - and the '" / "' surrounding the variable.
Alternatively, your echo with the link could look like this:
echo "<a href='recordInfo.php?record='{$array['RecordNumber']}'>{$array['RecordNumber']}</a>";
These accomplish the same thing.
This principle is the same, BTW, if your code looks like this:
$results = some_mysql_query;
while ($row = mysql_fetch_array($result)) {
...
echo $row['RecordNumber'];
Also, in case you're not already aware of how this will work, your recordInfo.php will receive its information in the $_GET array; specifically, it will refer to the RecordNumber as $_GET['RecordNumber'].

I want to print the output from DOMXPath in 2 colomns. 1. Car name 2. Price

I have tried the following code:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
$i = 0;
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
foreach($car_row as $row){
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $car_row2->nodeValue;
echo "</td></tr>";
}
echo "</tbody></table>";
You can't iterate over one array with foreach and expect the other to follow.
foreach essentially resets the current index on an array, then loops through calling next on the array until there are no elements left. Using reset and next you can emulate this for your second array as follows:
$car_row = $car_xpath->query('//h3[#class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[#class="snb_price_tag"]');
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
$row2 = reset($car_row2); // set the internal array pointer to the begining
foreach($car_row as $row) {
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $row2->nodeValue;
echo "</td></tr>";
$row2 = next($car_row2); // retrieve the next node from car_row2
}
echo "</tbody></table>";

Only LAST tuple displayed as HTML table is populated with delimited file with PHP

I have this type of record in an HTML table:
<tr class="simplehighlight" onclick="window.location.href='./games/game191.php';">
<td>06/09/2007</td><td>Jennifer Woods Memorial Grand Prix</td><td>C54</td>
<td>Nikolayev, Igor</td><td>2431</td><td>Parry, Matt</td><td>2252</td><td class="text-center">1-0</td></tr>
I want to read in a delimited file, make an array, and populate the table: (sample record)
game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry, Matt|2252|1-0
I tried this, but it only displays the last record from the datafile (/games.csv)
<?php
// open delimited data file "|" if needed and read.
//game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry, Matt|2252|1-0
if(!isset($_SESSION['games_array'])) {$file = $_SERVER['DOCUMENT_ROOT'].'/games.csv';
$fp = fopen($file,"r"); $list = fread($fp, filesize($file));
$_SESSION['games_array'] = explode("\n",$list); fclose($fp);}
// extract variables from each tuple by iteration
foreach ($_SESSION['games_array'] as $v);{
$token = explode("|", $v);
//write the table row and table data
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/";
echo $token[0]; echo ".php';\">";
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>";
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>";
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>";
echo "<td>";echo $token[7];echo "</td>";
echo "<td class=\"text-center\">"; echo $token[8];echo "</td>";
echo "</tr>";};
?>
What am I missing?
foreach ($_SESSION['games_array'] as $v);{
should be
foreach ($_SESSION['games_array'] as $v) {
Try with file():
$lines = file('filename');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
do you have the right permissions to access that file? is your php error_reporting set to display all errors? you could try using only relative paths, have you tried that? try using file_get_contents...
It was simpler than I thought... I erred in reusing old junk code from another script.
I found this and it works!
<?php
$text = file('games.csv'); foreach($text as $line) {$token = explode("|", $line);
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/";
echo $token[0]; echo ".php';\">";
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>";
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>";
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>";
echo "<td>";echo $token[7];echo "</td>";
echo "<td class=\"text-center\">"; echo $token[8]; echo "</td>";
echo "</tr>";};
?>
Let me know if you see any improvements or faster functions. Thank you !

Categories