Not sure why this is happening. The script below displays the title, link and custom meta of all pages using a specific template.
For some reason its duplicating the information.
Online example: http://greenleavesmarketing.co.uk/sherif/prices/
Script:
<?php // Display list of pages using the template page-landing.php
$product_pages_args = array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-landing.php',
'depth' => -1,
'hierarchical' => 0,
);
$product_pages = get_pages( $product_pages_args );
echo '<table class="price-table">
<thead>
<tr>
<td>Dental Procedure</td>
<td>Cost</td>
</tr>
</thead>';
foreach ( $product_pages as $product_page ) {
echo '
<tr>
<td>
'.$product_page->post_title .'
</td>
<td>'; ?>
<?php
$custom_metabox = get_post_meta($product_page->ID,'_custom_meta',TRUE);
if($custom_metabox['landing-para']!=false) {
echo $custom_metabox['landing-para'];
} else {
echo 'Please contact us for a free consultation.';
}
echo '</td>
</tr>';
}
echo '</table>'; wp_reset_query(); ?>
This doesn't necessarily fix the underlying problem, but if all $product_page->post_title values are unique, you could store them in an array as you go through the foreach loop and continue if the value already exists:
// create a dummy array to store $product_page->post_title values:
$post_titleArr = array();
foreach ( $product_pages as $product_page ) {
if (in_array($product_page->post_title, $post_titleArr)) continue; // skip this iteration for dupe.
$post_titleArr[] = $product_page->post_title; // not a dupe - add it to the array.
// continue your code...
echo '
Related
Hi guys i am trying to display the first td values as static so i have keep those values in one array and i try to increase the values and try to display but when i get it is displaying depending on foreach values if i have one record in foreach it is displaying one value and if i have 2 records it is displaying 2 values.
But i want to display all td value if i have values in foreach or not also.
Here is my code:
<tbody>
<?php
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion',3=>'a',4=>'b',5=>'c',6=>'d',7=>'e',8=>'f',9=>'g',10=>'h');
$i = 0;
foreach($getCourse as $report)
$status= $report->status;
{
?>
<tr>
<td><?php echo $arr[$i]; ?>
<?php $i++; ?></td>
<td><?php
if($status==1)
{
echo "On Hold";
}
elseif($status==2)
{
echo "Asset Incomplete";
}
elseif($status==3)
{
echo "Yet to Start";
}
elseif($status==4)
{
echo "SME Discussion";
}
elseif($status==5)
{
echo "Development";
}
elseif($status==6)
{
echo "PB Review";
}
elseif($status==7)
{
echo "PB Fixes";
}
elseif($status==8)
{
echo "PB2 Review";
}
elseif($status==9)
{
echo "PB2 Fixes";
}
elseif($status==10)
{
echo "Alpha Development";
}
elseif($status==11)
{
echo "Alpha Review";
}
elseif($status==12)
{
echo "Alpha Fixes";
}
elseif($status==13)
{
echo "Beta Review";
}
elseif($status==14)
{
echo "Beta Fixes";
}
elseif($status==15)
{
echo "Gamma";
}
?></td>
<td><?php echo $report->coursename; ?></td>
<td><?php echo $report->statuscount;?></td>
<td></td>
</tr>
<?php
}
?>
</tbody>
Here is my controller:
public function index()
{
if ($this->session->userdata('is_logged')) {
$data['getCourse']=$this->Report_model->getTopicReports();
$this->load->view('template/header');
$this->load->view('reports/report',$data);
$this->load->view('template/footer');
}
else {
redirect("Login");
}
}
Here is my model:
public function getTopicReports()
{
$this->db->select('count(t.status) as statuscount,t.topicName as topicname,c.coursename,t.status')
->from('topics t')
->join('course c', 't.courseId = c.id')
->where('t.courseid',1)
->where('t.status',5);
$query = $this->db->get();
return $query->result();
}
This is how i want to display here is my referrence image:
Can anyone help me how to do that thanks in advance.
FINAL UPDATED & WORKING VERSION
For me this was tricky. This turned out to be what I felt to be a awkward indexing issue.
The problem is that your data is not optimized very well to accommodate the task you are asking for. You have to make odd comparisons as you traverse the table. I was able to get it accomplished.
I would actually be very interested in seeing a more refined approach. But until that happens this code will dynamically generate a table that matches the output of the sample pictures that you posted in your question.
I have tested this code with some sample data that matches the array structure that you posted in your comments.
Here is the code:
//Create an array with your status values.
$rowName = array(
'On Hold',
'Asset Incomplete',
'Yet to Start',
'SME Discussion',
'Development',
'PB Review',
'PB Fixes',
'PB2 Review',
'PB2 Fixes',
'Alpha Development',
'Alpha Review',
'Alpha Fixes',
'Beta Review',
'Beta Fixes',
'Gamma'
);
//Generate a list of class names and get rid of any duplicates.
foreach($getCourse as $report){
$courseNames[] = $report->coursename;
}
$courseNames = array_unique($courseNames);
//Create the header.
echo
'<table>
<thead>
<tr>
<th>#</th>';
foreach($courseNames as $course){
echo '<th>' . $course . '</td>';
}
echo
'<th>Total</th>
</tr>
</thead>
<tbody>';
//Iterate across the array(list) of status values.
for($i = 0; $i < count($rowName); $i++){
echo
'<tr>';
echo '<td>' . $rowName[$i] . '</td>';
//Iterate through all combinations of class names and status values.
for($j = 0; $j < count($courseNames); $j++){
//Set flags and intial values.
$found = FALSE;
$total = 0;
$value = NULL;
for($k = 0; $k < count($getCourse); $k++){
//***Note - The ""$getCourse[$k]->status - 1" is because the status values in your data do not appear
//to start with an index of 0. Had to adjust for that.
//Sum up all the values for matching status values.
if(($getCourse[$k]->status - 1) == $i){
$total += $getCourse[$k]->statuscount;
}
//Here we are checking that status row and the status value match and that the class names match.
//If they do we set some values and generate a table cell value.
if(($getCourse[$k]->status - 1) == $i && $courseNames[$j] == $getCourse[$k]->coursename){
//Set flags and values for later.
$found = TRUE;
$value = $k;
}
}
//Use flags and values to generate your data onto the table.
if($found){
echo '<td>' . $getCourse[$value]->statuscount . '</td>';
}else{
echo '<td>' . '0' . '</td>';
}
}
echo '<td>' . $total . '</td>';
echo
'</tr>';
}
echo '</tbody>
</table>';
Try this. I am storing course status in $used_status then displaying the remaining status which are not displayed in foreach.
$arr = array ( '1' =>'On Hold', '2' => 'Asset Incomplete', '3' => 'SME Discussion', '4' => 'a', '5' => 'b', '6' => 'c', '7' =>'d', '8' => 'e', '9' => 'f', '10' => 'g', '11' => 'h' );
$used_status = array();
foreach($getCourse as $report) { ?>
<tr>
<td>
<?php $course_status = isset($arr[$report->status]) ? $arr[$report->status] : "-";
echo $course_status;
$used_status[] = $course_status; ?>
</td>
<td>
<?php echo $report->coursename; ?>
</td>
<td>
<?php echo $report->statuscount; ?>
</td>
</tr>
<?php }
foreach($arr as $status) {
if(!in_array($status, $used_status)) { ?>
<tr>
<td>
<?php echo $status; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
</tr>
<?php }
} ?>
I am having two set off arrays, first array is holding data about colors, second about dimensions.
$colors= array(
"27" => "RAL 9002",
"255" => "RAL 9006",
"341" => "RAL 8019",
"286" => "RAL 7016",
"141" => "RAL 3009",
"171" => "RAL 6028",
"121" => "RAL 8004",
"221" => "RAL 5010",
"101" => "RAL 3000",
"273" => "RAL 9007",);
$dimensions = array
(
array(0.3,1245),
array(0.35,1245),
array(0.40,1100),
array(0.45,1245),
array(0.50,1245),
array(0.60,1245),
array(0.70,1245),
);
Values above are used for queries. I wanted to make an array which will hold data, and later in comparison will print data. Query can return result NULL
foreach($colors as $key => $value)
{
//print "Boja $key . <br>";
foreach($dimensions as $data )
{
//print "Debljina $data[0], Sirina $data[1] Boja $key <br>";
$sql = "SELECT Debljina, Sirina, sum(Kolicina) as suma
FROM jos_ib_repromaterijali WHERE Debljina = '$data[0]' AND Sirina = '$data[1]' AND Boja = '$key'";
$q = $conn -> query($sql);
$vrijednosti = array();
while($r=$q->fetch()) {
$debljina = $r['Debljina'];
$sirina = $r['Sirina'];
$kolicina = $r['suma'];
$vrijednosti[] = $debljina . $sirina . $kod . $kolicina;
}
}
}
Once I get results, I create html table
<div class="col-lg-6">
<table class="table table-bordered">
<thead>
<tr>
<th><?php echo "Dimenzije"; ?> </th>
<?php foreach($colors as $boja) { ?>
<th><?php echo $boja; ?> </th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($dimensions as $dim) {
?>
<tr>
<td><?php echo $dim[0] . ' X ' . $dim[1]; ?> </td>
<td><?php
if (isset($vrijednosti[$dim[0] . $dim[1]])) {
echo "asdas";
}
else {
echo "error";
}
?> </td>
</tr>
<?php }
?>
</tbody>
</table>
Table print headers and dimensions as it should, I am having trouble in matching rows with column. If someone give me an advice it would be appreciated.
Query can give result NULL; if that is result It should print 0 below color for specific row.
I am writing helper functions that allow me to build tables quickly. I have a function called responsive_table that accepts two parameters: headings, and rows. The rows are dynamic and are pulled from a database. I build an array of rows with a foreach loop because some of the db table rows don't match the contents or order of the html table... some of the cells in the html table contain combinations of db columns, etc. and can't be displayed directly from the db. None of the rows in my HTML table array are being displayed. Nothing from inside of the function that's being declared in the function parameter is being displayed. Here's my code:
<?php
$output = responsive_table(array('<input type="checkbox" name="all">', 'Thumbnail', 'Title', 'Location', 'Author', 'Date Submitted', 'Actions'), function(){
foreach($listing_results as $listing){
$listings[] = array(
'<input type="checkbox" name="delete[]" value="'.$listing['listing_id'].'">',
'<img src="http://placehold.it/50x50" alt="Thumbnail">',
$listing['title'],
$listing['location'],
anchor('members/modify/'.$listing['member_id'], $listing['display_name']),
date($setting['date_format'], $listing['date_submitted']),
array(
array(
'title' => 'Modify Listing',
'color' => 'primary',
'href' => 'listings/modify/'.$listing['listing_id'],
'text' => '<i class="fa fa-pencil"></i>'
),
array(
'title' => 'Delete Listing',
'color' => 'danger',
'href' => 'listings/delete/'.$listing['listing_id'],
'text' => '<i class="fa fa-eraser"></i>',
'confirm' => true
)
)
);
}
return $listings;
});
echo $output;
function responsive_table($headings = array(), $rows = array(), $sortable = false){
$output = '
<div class="table-responsive">
<table data-sortable class="table">
<thead>
<tr>
';
if(count($headings) > 0){
foreach($headings as $heading){
$output .= '
<th>'.$heading.'</th>';
}
}
$output .= '
</tr>
</thead>
<tbody>';
if(count($rows) > 0){
foreach($rows as $row){
$output .= '
<tr>
<td>'.$row.'</td>
</tr>';
}
}
$output .= '
</table>
</div>';
return $output;
}
Yes, what you are looking for is a http://php.net/manual/en/class.closure.php
Any valid PHP code may appear inside a function, even other functions and class definitions. What it looks like your trying to do is work with anonymous functions, also known as closures. Here is a link that might help you out with this PHP anonymous functions.
I have a 25 advertiserids from CJ and now I want to create 2 post in different category of wordpress from each advertiserid given. So I have created following script but it is not pausing for user input, so how can I do that ?
If its not possible with while then is there any other method to do this ? in script $ad is array with advertiser's id value and $adcat is also a array with advertiser's catagory
function cjlinks($n)
{
global $ad, $adcat;
$URI = 'https://product-search.api.cj.com/v2/product-search?website-id=12345678'.
'&advertiser-ids='.$ad[$n].
'&records-per-page=2';
$context = stream_context_create(
array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: ' .
'my api id'
)
));
$res = new SimpleXMLElement(file_get_contents($URI, false, $context));
return $res;
}
$a = 0;
while ($a < 25)
{
echo 'advertiser id is: '.$ad[$a].'<br/>advertiser - catagory is: '.$adcat[$a]->child.
'<br/>';
if (isset($_SESSION['sumit'])){
$data = cjlinks($a);
$attributes = $data->products->attributes();
if ($attributes->{'total-matched'} == 0){
echo 'No products found ...try again with new keyword.';
}else{
foreach ($data->products[0] as $product)
{
// Sanitize data.
$price = number_format((float)$product->price, 2, '.', ' ');
$image = '<img src="'.$product->{'image-url'} .'" style="float: right"/>';
$pd = $image.$product->description .'<a href="'.$product->{'buy-url'}.
'">...For more details and to buy it click here</a>';
$p = array('post_title' => $product->name,
'post_content' => $pd,
'post_status' => 'publish',
'post_author' => 1,
'post_category' =>array($_GET['cat']));
$pr = wp_insert_post( $p, $wp_error );
echo '<br/>posted...post ID is:'.$pr;
wp_reset_query(); // Restore global post data stomped by the_post().
}
}
}else{
echo 'please complete form';
$a = $a+1;
}
}
?>
<html>
<body>
<form action="catag.php" method="get">
<table>
<tr>
<td><label> Select a catagory from list:</label></td></tr>
<tr>
<?php
foreach($cat as $key=>$val){
echo '<tr><td><input type="radio" value="'.$val->cat_ID.'" name="cat" id="'.$val->cat_ID.'">'.$val->cat_name.'</td></tr>';
}
?>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form><br>
</body>
</html>
You can not literally "pause" a php script, as php is executed on the server before the page even loads.
To execute any kind of a "pause" you would need to write your function in Javascript or other Client Side (Browser Executed) code, or send something like an Ajax request to a php page that would then update the current page on response.
I am writing a shopping cart and have my data stored in the $_SESSION array, but would like to calculate a total. below it the code I thought would work to do this, but it returns '1' in stead of a total!
$total = array($_SESSION['qty'],$_SESSION['pr']);
/* I'll give you more code...thanks for your help!!
here is the code for my php cart:
<?php
function item_list()
{
if(isset($_SESSION['qty'])){
$total = array($_SESSION['qty'],$_SESSION['pr']);
foreach($_SESSION['qty'] as $key => $value)
{?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo array_product($total); ?>
</tr><?php
}
}
}
session_start();
if(isset($_POST['clear']) && ($_POST['clear'] == 'clear'))
{
session_destroy();
unset($_SESSION['qty']);
unset($_SESSION['item']);
unset($_SESSION['pr']);
unset($_POST['qty']);
unset($_POST['item']);
unset($_POST['pr']);
}
if(!isset($_SESSION['qty'])) $_SESSION['qty'] = array();
if(!isset($_SESSION['item'])) $_SESSION['item'] = array();
if(!isset($_SESSION['pr'])) $_SESSION['pr'] = array();
if(isset($_POST['qty']))
{
foreach($_POST['qty'] as $value)
{
if(!$value == '') array_push($_SESSION['qty'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
foreach($_POST['item'] as $key => $value)
{
if(!$_POST['qty'][$key] == '') array_push($_SESSION['item'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
foreach($_POST['pr'] as $key => $value)
{
if(!$_POST['qty'][$key] == '') array_push($_SESSION['pr'], filter_var($value,
FILTER_SANITIZE_SPECIAL_CHARS));
}
}
?>
That is a strange way to structure a shopping cart, but here's how to do it with that structure:
foreach($_SESSION['qty'] as $key => $value)
{
$total = $_SESSION['qty'][$key] * $_SESSION['pr'][$key];
?>
<tr>
<td align="center"><?php echo $_SESSION['item'][$key]; ?></td>
<td align="center"><?php echo $value; ?></td>
<td align="center"><?php echo $_SESSION['pr'][$key]; ?></td>
<td align="center"><?php echo $total; ?>
</tr><?php
}
If you wanted to get a total of all quantity and cost of the cart:
function getTotals()
{
$total = array('qty' => 0, 'price' => 0);
foreach($_SESSION['qty'] as $key => $qty)
{
$total['qty'] += $qty;
$total['price'] += ($_SESSION['pr'][$key] * $qty)
}
return $total;
}
$total = getTotals();
echo $total['qty']; // output the total quantity of items
echo $total['price']; // output the total cost for all items and quantity
I would recommend a better structure though, something like:
$_SESSION['cart']['items'] = array(
array(
'name' => 'Screwdriver',
'price' => 5,
'qty' => 2,
),
array(
'name' => 'Hammer',
'price' => 10,
'qty' => 1,
)
);
As per your cart array it is not able to hold multiple products you have to use multy dimensional array like this
$_SESSION['cart_items'] = array(
array( "qty"=>5, "item"=>"tshirt", "pr"=>50.20),
array( "qty"=>2, "item"=>"Cell Phone", "pr"=>50.20),
array( "qty"=>7, "item"=>"", "pr"=>50.20),
)
then you can write your code like this
function item_list()
{
foreach($_SESSION['cart_items'] as $item_array)
{?>
<tr>
<td align="center">Item:<?php echo $item_array['item']; ?></td>
<td align="center">Qty: <?php echo $item_array['qty']; ?></td>
<td align="center">Price :<?php echo $item_array['pr']; ?></td>
<td align="center">Total : <?php echo $item_array['qty'] * $item_array['pr']; ?>
</tr><?php
}
}
You should create yourself a Card class that is able to import/export data from the $_SESSION superglobal (or some other array if you mock it for tests, testing with $_SESSION can be akward) which is able to handle your data-structure easily and can calculate the total, too:
$cart = new Cart();
$cart->importFromArray($_SESSION);
// or:
$cart->importFromArray($_SESSION['cart']);
// later on:
$total = $cart->getTotal();
// somewhere else:
$cart->addItem(...);
...
$_SESSION['cart'] = $cart->exportToArray();
That will allow you to more easily change the code over time.