PHP - Product Loop Excluding Values With Same Name
Hello, I need to hide products with same title from WooCommerce shop page and i'm trying to achieve that with a custom loop. For example i have 5 products called "Plex" and the only diferrence between them is the SKU field and the color. I don't care which of the same product will be displayed in the shop page, i just want to display only one of all these products. I created a loop which is working and does hide products with same name but i have a problem. If i set posts_per_page=8 it shows less because it counts the hidden products also. $args = array( 'post_type' => 'product', 'posts_per_page' => 8 ); $query = new WP_Query($args); $list = array(); while ($query->have_posts()) : $query->the_post(); if(in_array(get_the_title(), $list)){ continue; } $list[] = get_the_title(); ?> <li><?php wc_get_template_part( 'content', 'product' ); ?></li> <?php endwhile; wp_reset_postdata(); P.S. I don't want to remove these products, i just want to hide them from shop loop! Any ideas what is the problem with the loop? Is there a better way to achieve that? Similar TutorialsHi there, I'm trying to get the full Product Name in the <title> of each of the product pages - I can't seem to figure out what to put in the product_info.php... I've tried a few different options but I'm not great with php Here's what I have currently but it creates errors... <script language="javascript"><!-- document.write('<title><?php echo TITLE; ?> : <?php echo $product_info['products_name']; ?> </title>'); //--></script> Hope someone can help - thanks so much! Hi, I have a database with some rows. in each row there is a number such as: 0.0023 or 0.0135... I have the following code to loop though them and add the numbers up but it always returns 0? Code: [Select] $sql="SELECT * FROM tickets WHERE `round` = '$round' AND `confirmed` = 'yes' ORDER BY id ASC"; $result=mysql_query($sql); $row=mysql_fetch_array($result); while($row = mysql_fetch_array( $result )) { $balance = $balance + $row['amount']; } Hi.. I have table which has a data so_month: FromMonth : 5 ToMonth : 7 and I have table working_days: MonthName May Jun Jul MonthNumber 05 06 07 WorkingDays 23 24 23 Now I have function to get the 3 consecutive months from FromMonth to ToMonth , which as you can see from May to Jul Now I have problem in getting the SUM of Working days. here is my code: <?php $sql = "SELECT FromMonth, ToMonth FROM so_month"; $res = mysql_query($sql,$con); $row = mysql_fetch_assoc($res); $FromMonth = $row['FromMonth']; $ToMonth = $row['ToMonth']; function monthNames($from, $to){ $range=array(); for($i=$from; $i<=$to; $i++){ $range[$i]=date('M', mktime(0,0,0,$i)); } return $range; } $month_ = implode("' ', ",monthNames($FromMonth,$ToMonth)); foreach( monthNames($FromMonth, $ToMonth) as $month){ $sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName"; $res = mysql_query($sql, $con); while($row = mysql_fetch_array($res)){ $WorkingDays = $row['WorkingDays']; } echo $WorkingDays; } ?> the output of this code is: 232423 and when I change this line: $WorkingDays = $row['WorkingDays']; to $WorkingDays += $row['WorkingDays']; the output is: 234770 I correct output should be: 70 Any help is highly appreciated. Thank you very much.. Hope someone can help, I am trying to add up all the values of a variable in a PHP while loop and have tried several approaches but none seem to work. Hopefully someone can point out what the correct method is. I've set up some code to pull the pay rate from a MySQL table, and then calculate that person's earnings based on the amount of time they spend on a particular job. That works fine:
However there may be a number of jobs for different people with different rates, so I want to add all of these up and output the total. So for example if I had values of 125.00, 35.50 and 22.75 I want to show 183.25 as the total. So I tried a few things I found on forums on the subject:
That didn't work so I tried another method that someone suggested:
The 2nd method outputted just the last individual value, the 1st method didn't work at all. Does anyone have a working method that can be used to do this? Thanks Is it possible to use for loop to insert values into database using for loop? Or what would be the another way to insert these values into the database if the number of values being entered differs from one time to another? Here is basically what I am trying to do: mysql_query("INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDate', DateTime='$timestamp', for($i=1; $i<=$SetNumber; $i+=1){ for($j=0; $j<7; j+=1){ $NumberName="Number".$i.$v[$j];//Generating name of database field $$NumberName=$Match[0][$j];//Here I would store the $Match value into the fieldname name created above } } Tries='$RunCounter'"); I am trying to get info from a table, add rows in that table, and put the added value back into the table in another field...I want to add field1 and field2 of every row and put the total in the total row. Heres what the echo gives me Susan 14 Mary 18 Bob 13 Sam 21 heres the database mysql> SELECT total_val FROM table; +-----------+ | total_val | +-----------+ | 21 | | 21 | | 21 | | 21 | +-----------+ heres the code.... <?php require("connection.php"); mysql_select_db("database", $connection); echo "<br />"; $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $values = ($row['field1'] + $row['field2']); $sql=mysql_query("UPDATE table SET total = '$values'"); echo $row['user_name'] . " " . $values; echo "<br />"; } ?> also note that if I put a INT in place of $values - the echo changes to match...why?? PLEASE help...I have been working on this for 2 entire days.... First of all i create the page to copy the buttons as pixels to paste into a design, it's not for a working webpage. It goes wrong here, <option value="0">Day</option> <?= for(var i=0; i<31; i++){?> <option value="<?=i?>"><?=i?></option> <?= } ?> </select> Code: [Select] <?php ?> <html> <head> <title>Community Development Project</title> </head> <body> <div id="container"> <form id="opties1" name="opties1" method="post" action="something.php"> <div class="forminput"> <input type="button" id="signin" name="signin" value="Sign In" /> <br/> <br/> <input type="button" id="register" name="register" value="Register" /> <br/> <br/> <input type="button" id="uploadimage" name="uploadimage" value="Upload Image" /> <br/> <br/> <input type="button" id="next" name="next" value="Next" /> <br/> <br/> <input type="button" id="addanother" name="addanother" value="Add Another" /> <br/> <br/> <input type="button" id="register" name="register" value="Register" /> <br/> <br/> <input type="button" id="create" name="create" value="Create" /> <br/> <br/> <input type="button" id="addfolder" name="addfolder" value="Add Folder" /> <br/> <br/> <input type="button" id="addimage" name="addimage" value="Add Image" /> <br/> <br/> <input type="button" id="addtask" name="addtask" value="Add Task" /> <br/> <br/> <input type="button" id="cancel" name="cancel" value="Cancel" /> <br/> <br/> <input type="button" id="watch" name="watch" value="Watch" /> <br/> <br/> </div> </form> <form id="opties2" name="opties2" method="post" action="something.php"> <div class="forminput"> <select id="day" name="day"> <option value="0">Day</option> <?= for(var i=0; i<31; i++){?> <option value="<?=i?>"><?=i?></option> <?= } ?> </select> <select id="country" name="Country"> <option value="0">Netherlands</option> <option value="1">Germany</option> <option value="2">Belgium</option> </select> </div> </form> </div> </body> </html> Hi there, I am working on a PHP form and running a loop to generate textboxes at runtime. This code output the textboxes: foreach ($MyTest as $tst) { echo "<input name='testName' type='text' size='12' maxlength='5' />"; } I am able to display (or output) 4 or sometimes 6 textboxes depending on the variable $MyTest. Now after I enter the values in these texboxes. Lets say, it outputs 3 textboxes, I woud like to display the values like: In Textbox 1 you entered: In Textbox 2 you entered: In Textbox 3 you entered: How can I get (or fetch) values from the textboxes generated through the loop. Please reply. All comments and feedbacks are always welcomed! Thank you Hi I am new this forum and looking for some much appreciated help with something that has me stumped. I have extracted regions from image names in a directory. example: 'edinburgh_castle_(Edinburgh).jpg' (Extracted Edinburgh from in-between the brackets) So I have a list of Regions, extracted from all the various image names in the directory which I want to populate into a dynamic drop down menu for filtering purposes. Here's my issue... I want there to be only one 'Edinburgh' option appearing in my drop down menu. I don't want duplicates. Here is my code so far. php: [Select] Please see the following code:
$colors = ["red hello","green this","blue lovely","yellow morning"]; foreach ($colors as $towns) { $hello = (explode(" ", $towns)); }If I do print_r($hello[1]); within the loop I get all the values of index [1] in the browser although not sure why there are no spaces between the words: HelloThisLovelyMorning However if I do the same print outside the loop I only get this displaying in the browser: Morning That's no good to me because I want all the values with the index of [1] so I can use them anywhere I want. Any help would be much appreciated.
hi, i’m currently facing a new problem (i managed to use array_unique function successfully but i face this problem)with my php loop. i couldn’t seem to increment in my foreach loop to get the value of an array. $update_id1=Array ( [0] => 398 [1] => 397 [2] => 393 [3] => 391 [8] => ); //$i=0; foreach ($update_id1 as $i => $v) { $ex= explode(",", $v); $unique=array_unique($ex); //f$ketch username from update table in db and inject it to the feed query. $imp_id= implode(',', $ids); //$id1=$ids[$i]; // $x=0; // for($x=0;$x<count(unique);$x++){ echo 'ids:- '.$i; print_r($unique[$i]); echo '<br>'; //} $totalUpdates=$project->totalUpdates($ids[$i],$_SESSION['uname'] ,$unique[$i],$load); //$i++; } i tried every method nothing seems to work can anyone help me in this regard please. Edited December 25, 2019 by narutofanHi friends. I want to php code on how i can modify my retrieved database values from a particular table before echoing out. Please could you help me correct the code. Or if there is any better way of editing loop datas before printing out, please kindly drop the code for me. Thanks <?php please if there is any better way of editing loop values from mysql database before printing out, please kindly drop the code for me. Thanks I have my data array which i converted to json by splitting the keys using for each loop. Data:
$data = Array ( [table6-ios-camt] => [table6-ios-iamt] => [table6-ios-samt] => [table6-ios-type] => [table6-isd-camt] => 0 [table6-isd-iamt] => 0 [table6-isd-samt] => 0 [table6-isd-type] => [table6-ios-csamt] => [table6-isd-csamt] => 0 [table6-other-camt] => [table6-other-iamt] => [table6-other-samt] => [table6-other-type] => [table6-tran1-camt] => 0 [table6-tran1-iamt] => [table6-tran1-samt] => 0 [table6-tran1-type] => [table6-tran2-camt] => 0 [table6-tran2-iamt] => [table6-tran2-samt] => 0 [table6-tran2-type] => [table6-itc_3b-csamt] => 0 [table6-dump_iog-camt] => [table6-dump_iog-iamt] => [table6-dump_iog-samt] => [table6-dump_iog-type] => [table6-dump_oth-camt] => 4386.09 [table6-dump_oth-iamt] => [table6-dump_oth-samt] => 4386.09 [table6-dump_oth-type] => [table6-itc_clmd-camt] => [table6-itc_clmd-iamt] => [table6-itc_clmd-samt] => [table6-itc_clmd-type] => [table6-total_10-camt] => 4386.09 [table6-total_10-iamt] => 0 [table6-total_10-samt] => 4386.09 [table6-total_10-type] => [table6-total_11-type] => [table6-total_8-csamt] => 0 [table6-total_9-csamt] => 0 [table6-dump_iog-csamt] => [table6-dump_isrc-camt] => [table6-dump_isrc-iamt] => [table6-dump_isrc-samt] => [table6-dump_isrc-type] => [table6-dump_oth-csamt] => [table6-iog_goods-camt] => [table6-iog_goods-iamt] => [table6-iog_goods-samt] => [table6-iog_goods-type] => Capital Goods [table6-itc_clmd-csamt] => [table6-total_10-csamt] => 0 [table6-dump_isrc-csamt] => [table6-iog_goods-csamt] => [table6-iog_inputs-camt] => [table6-iog_inputs-iamt] => [table6-iog_inputs-samt] => [table6-iog_inputs-type] => Input Services [table6-iog_inputs-csamt] => [table6-iog_services-camt] => [table6-iog_services-iamt] => [table6-iog_services-samt] => [table6-iog_services-type] => Input Services [table6-supp_non_rchrg_goods-camt] => [table6-supp_non_rchrg_goods-iamt] => [table6-supp_non_rchrg_goods-samt] => [table6-supp_non_rchrg_goods-type] => Capital Goods [table6-supp_rchrg_reg_goods-camt] => [table6-supp_rchrg_reg_goods-iamt] => [table6-supp_rchrg_reg_goods-samt] => [table6-supp_rchrg_reg_goods-type] => Capital Goods [table6-supp_non_rchrg_goods-csamt] => [table6-supp_non_rchrg_inputs-camt] => 4386.09 [table6-supp_non_rchrg_inputs-iamt] => [table6-supp_non_rchrg_inputs-samt] => 4386.09 [table6-supp_non_rchrg_inputs-type] => Inputs [table6-supp_rchrg_reg_goods-csamt] => [table6-supp_rchrg_reg_inputs-camt] => [table6-supp_rchrg_reg_inputs-iamt] => [table6-supp_rchrg_reg_inputs-samt] => [table6-supp_rchrg_reg_inputs-type] => Input Services [table6-supp_non_rchrg_inputs-csamt] => [table6-supp_rchrg_reg_inputs-csamt] => [table6-supp_rchrg_unreg_goods-camt] => [table6-supp_rchrg_unreg_goods-iamt] => [table6-supp_rchrg_unreg_goods-samt] => [table6-supp_rchrg_unreg_goods-type] => Capital Goods [table6-supp_non_rchrg_services-camt] => [table6-supp_non_rchrg_services-iamt] => [table6-supp_non_rchrg_services-samt] => [table6-supp_non_rchrg_services-type] => Input Services [table6-supp_rchrg_reg_services-camt] => [table6-supp_rchrg_reg_services-iamt] => [table6-supp_rchrg_reg_services-samt] => [table6-supp_rchrg_reg_services-type] => Input Services [table6-supp_rchrg_unreg_goods-csamt] => [table6-supp_rchrg_unreg_inputs-camt] => [table6-supp_rchrg_unreg_inputs-iamt] => [table6-supp_rchrg_unreg_inputs-samt] => [table6-supp_rchrg_unreg_inputs-type] => Input Services [table6-supp_non_rchrg_services-csamt] => [table6-supp_rchrg_reg_services-csamt] => [table6-supp_rchrg_unreg_inputs-csamt] => [table6-supp_rchrg_unreg_services-camt] => [table6-supp_rchrg_unreg_services-iamt] => [table6-supp_rchrg_unreg_services-samt] => [table6-supp_rchrg_unreg_services-type] => Input Services [table6-supp_rchrg_unreg_services-csamt] => ); json: { "table6":{ "ios":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "isd":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "other":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran1":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "tran2":{ "camt":0, "iamt":0, "samt":0, "csamt":0 }, "itc":{ "camt":0, "iamt":550, "samt":230, "csamt":680 }, "iog":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_non_rchrg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":4386.09, "samt":4386.09, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_reg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ], "supp_rchrg_unreg":[ { "itc_typ":"cg", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"ip", "camt":0, "samt":0, "iamt":0, "csamt":0 }, { "itc_typ":"is", "camt":0, "samt":0, "iamt":0, "csamt":0 } ] } }
Keeping in mind the following conditions: 1.If all the keys in the objects are 0 then the entire object will be removed (easy part). 2.If a key in the object has any value other than 0 , the object should not be removed and the key with "0" value should also be there.i've tried splitting value and using conditions to check for 0 or null values but the problem with that is it removes all keys that have 0 values thus failing condition 2 for the code. $vars = $data; $count = ""; $finalArray = array(); foreach ($vars as $keys => $values) { foreach ($values as $k => $v) { foreach ($v as $ke => $ve) { if ($keys == "table6") { $count = (array)$v; foreach ($count as $ck => $cv) { if ($cv != "0") { $flag = true; break; } } if($flag == false) { array_push($finalArray, $count); } } } } print_r($finalArray); die(); I expeced the output to contain the objects with key value pairs that does not contain 0 value but everything prints here. I have a database query set up that returns an array. I then cycle through the rows in a foreach statement that wraps each value in a <td> tag to output it to a table. It works really great, but now I need to access two of the values and add some info to them. The first field returned is an image filename and I want to wrap it in an image tag that also concatenates the image path location to the value. All the images are stored in the same location, so basically I want to take the first value form the array and add the path to where the images are and wrap the whole thing in an <img> tag. The other field I need to modify is the last value in the array which is actually an email address. For that field I want to make them an active email link so I need to wrap an <a>href=mailto: </a> tag around the value. How could I modify this code so I could add the necessary tags to these elements of the array? Here is the code I am running: Code: [Select] $query = "Select member_image as 'Image', member_name as 'Name', city as 'City', phone as 'Phone', email as 'Email' FROM directory"; //connect to database $conn=db_connect(); //call function do_query to run the query and output the table do_query($conn, $querey); The functions called are as follows: Code: [Select] function db_connect() { $conn = new mysqli("localhost", "username", "password", "databasename"); } function do_query($conn, $query); { $result = mysqli_query($conn, $query); WHILE ($row = mysqli_fetch_assoc($result)) { If (!$heading) //only do if header row hasn't been output yet { $heading = TRUE; //so we only do it once echo "<table>\n<tr<>\n"; foreach ($row as $key => $val) { echo "<th>$key</th>"; } echo "</tr>\n"; } echo "<tr>"; foreach ($row as $val) { echo "<td> $val </td>"; } echo "</tr>\n"; } //close the while echo "</table>\n"; } Lets say I have an array of different values... for example, $alreadyViewed = array("hi.jpg","cow.jpg","dog.png"); Lets say my database contains: Code: [Select] animals.jpg cat.jpg cow.jpg hi.jpg dog.png How do you I tell MySQL to only fetch the remaining entries? And ignore everything in the $alreadyViewed array? My application displays posts based on currentness and popularity, in two distinct lists. Users can also vote on posts, after which they are hidden. The post IDs that users have voted on are stored in a cookie. I need to construct a concise query that prevents "popular" posts and posts that have been voted on from appearing within the "new" list. In my current system, the used values are explicitly excluded from appearing in the query with some very inelegant code. $num_posts = 5; $sql = "SELECT * FROM posts ORDER BY popularity LIMIT ".$num_posts; $query = mysql_query($sql); $excluded_ids = array() while($row = mysql_fetch_assoc($query)) { // ...do some stuff... $excluded_ids[] = $row['post_id']; } if(!empty($_COOKIE['voted_on'])) { // example value "12|19|23|26" foreach(explode("|", $_COOKIE['voted_on']) as $val)) { $excluded_ids[] = $val; } } $sql = SELECT * FROM posts WHERE post_id <>"; $num_excluded_ids = count($excluded_ids); $i = 0; foreach($excluded_ids as $val) { $i++; $sql .= $val; if($i <= $num_excluded_ids) { $sql .= " AND post_id <> "; } } $sql .= " ORDER BY date_posted LIMIT ".$num_posts; $query = mysql_query($sql) // ... This method is ugly and probably very slow. What would be a better approach? Thanks in advance for anything you can suggest. Hi, I'm sorry to bother you with this request for help. I bought a XML driven Flash website template with an integrated contact form. Even though the contact.php seems pretty secure I receive emails telling me that someone tried to send emails but they contained no recipient addresses, and therefore no delivery could be attempted. So far, it seems as if the validation process in the php and preg_match command seem to work. Never the less those messages keep getting on my nerve. The Failed delivery emails I get look like this: ---------- Date: Mon, 20 Dec 2010 08:12:09 +0100 Message-Id: <E1PUZuj-0005zp-8c@mydomain.com> To: Subject: X-PHP-Script: www.mydomain.com/contact.php MIME-Version: 1.0 Content-type: text/html; charset=windows-1251 From: ---------- As you can see spam protection by filtering the email characters with preg_match has its limits too. The interesting thing is that the sender (E1PUZuj-0005zp-8c) is (of course) changing but only in the last digits. I received about 200 emails with exact the same beginning. So the bot seems to follow an ongoing pattern. I wonder now, if there is a way to set a command to exclude (or to preg_match the exact) character combinations like "E1PUZ" which lead to not sending the email? I know ... it is only a matter of time till the combinations are different but altering the php code once a month is not as much work as deleting all those failed delivery messages per day. Hi Guys, I've been using PHP for a few years now but for some reason I can never seem to get my head around arrays, basically I have a blog site that I want to show all posts that have been posted in public categories but ignore those in the private categories, I had the idea to put all the private category ID's into an array and use that to check against the posts, however my array isn't working out the way I'd hoped. This is what I have: $isPrivate = mysql_query("SELECT categoryID FROM mod_cat WHERE isPrivate =1") or die (mysql_error()); while ($isPrivateList = mysql_fetch_array($isPrivate, MYSQL_NUM)) { $ignoreList = $isPrivateList; } However this isn't working at all. I know its wrong I just can't work out how to make it right. This is the mySQL I want to use it with: mysql_query("SELECT COUNT(*) AS total_entries FROM mod_posts WHERE articlePosted =1 AND articleCat != $ignorelist") Could anyone please help me? Thanks in advance. When I run this query to exclude records where id = $userarray['id'], $q = "SELECT * FROM users WHERE inst_id = '". $userarray['inst_id'] ."' AND id != '". $userarray['id'] ."' "; it actually ONLY returns records with $userarray['id'] as opposed to excluding them. Isn't the way to exclude them to use != ? |