PHP - How To Group Countries Into Regions?
I am working on a site, on a shipping profile module that requires users to group the countries into regions to ship the items to. That is:
Region 1 - Japan, USA, Spain, Italy. Region 2 - Belgium, France, Germany. I have a table that has all the Country names which will be populated in a Listbox control for users to select. Once the supplier has selected a group of countries from the Listbox, how can I properly save it into the database with its region name. Do I save multiple county IDs with comma or whats the right way. Please advise. The question maybe basic but I need your feedback. Thank you! Similar TutorialsBasically, I am creating a list of all the regions in the world, and I want it to display the countries that are inside each region. I am saving the data in a table that is composed of 2 columns, region name and country name. I have to do it this way, because there are some countries that fall into several regions. The link is here http://128.123.242.201/countries.php. As you can see it is repeating regions (as it should) but I don't know how to combine all the countries in each region together, and only display each geographical region once. Code: [Select] <!DOCTYPE html> <?php require "connect.php";?> <html> <head> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script> $(document).ready(function() { $("#accordion").accordion(); }); </script> </head> <body style="font-size:75%;"> <?php if(isset($_POST['submit'])) { $region = $_POST['region']; $country = $_POST['country']; $query= "INSERT INTO regions (region,country) VALUES ('$region','$country')"; $result = mysql_query($query); if(!$result) { $confirmMsg = "There was a problem updating the record."; } elseif(mysql_affected_rows()==0) { $confirmMsg = "There was no record to update."; } else { $confirmMsg = "The record was successfully updated."; } echo $confirmMsg; } ?> <h3> S<br />Add information: <form method="post" action =""><br /> Country: <input type = "text" name="country" /> Region: <input type = "text" name="region" /> <br /> <input type = "submit" value = "submit" name="submit" /> </form> </h3> <div id="accordion"> <?php //Delcare variable $info $region = $country = NULL; $query = "SELECT * FROM regions"; $result = (mysql_query($query)); while ($row=mysql_fetch_assoc($result)) { $region = "{$row['region']}"; $country = "{$row['country']}"; $deleteimg = "http://www.veryicon.com/icon/png/System/Float/Delete.png"; $delete = "<a href=\"delete.php?region=".$region."&country=".$country."\"onclick=\""; $delete .= "return confirm('Are you sure you want to delete?')\">"; $delete .= "<img src=".$deleteimg." height=\"20\"/></a>"; $accord = "<h3><a href=\"#\">$region</a></h3>"; $accord .= "<div>"; $accord .= "<p>"; $accord .= $delete.$country; $accord .= "</p>"; $accord .= "</div>"; echo $accord; } ?> </div> </body> </html> This topic has been moved to Apache HTTP Server. http://www.phpfreaks.com/forums/index.php?topic=359622.0 Hi, guys. I want to present different ads to visitors in different countries. For example, if a visitor is in Australia, I want him/her to see a different ad from, say, a visitor from the UK. Can this kind of thing be done with PHP? If so, will installing such a code slow down my site? Any help will be appreciated very much. I have an HTML list of locations, which I want to separate out into countries and cities, then put in an HTML menu with cities as submenu items of countries. My method is to turn the locations into two arrays, the original one with HTML links and the second one with just the name of the location, see if location matches an array of country name, and parse accordingly. I've done it successfully with a test array of locations $countries= array('Angola','Australia','Brazil','Croatia','France','Germany','Iceland','Italy','Kenya','Monaco','Panama','Romania','Switzerland','Tanzania','USA'); $test_location= array('Angola','Luanda','Australia','Brazil','Rio','Minas','Sao Paulo','Croatia','France', 'Paris'); // used in test, and works But my script doesn't work with the real html data.eg <li><a href="http://mysite.com/category-brazil.php" class="blog-category-link-enabled">Brazil (10)</a></li> <li><a href="http://mysite.com/category-igua00e7u.php" class="blog-category-link-enabled">Igua&#231;u (9)</a></li> <li><a href="http://mysite.com/category-rio-grande-do-sul.php" class="blog-category-link-enabled">Rio Grande do Sul (4)</a></li> <li><a href="http://mysite.com/category-germany.php" class="blog-category-link-enabled">Germany (12)</a></li> <li><a href="http://mysite.com/category-france.php" class="blog-category-link-enabled">France (12)</a></li> <li><a href="http://mysite.com/category-cote-d0027azur.php" class="blog-category-link-enabled">Cote d'Azur (2)</a></li> This part of the script is to identify two consecutive countries and print the first. It works with $test_location but doesn't with $location_short in the final if clause. If I can get this to wotk then I think the rest (not included here) will work too. <?php //$locations_categories is a long string of html links, separated by commas $location = explode(",", $locations_categories); // turns locations into an array of links - works, tested by print $location_count= count ($location); //works, tested by print $countries= array('Angola','Australia','Brazil','Croatia','France','Germany','Iceland','Italy','Kenya','Monaco','Panama','Romania','Switzerland','Tanzania','USA'); $test_location= array('Angola','Luanda','Australia','Brazil','Rio','Minas','Sao Paulo','Croatia','France', 'Paris'); // used in test, and works $test_location_count= count ($test_location); //works //// finds two consecutive countries and prints first //// for ($u=0; $u < $location_count; $u++) { $v = $u+1; $w = $u-1; $location_link[$u] = $location[$u]; //keeps copy of array with links and numbers, works //make array with just country names. No html. For matching against $countries. Retains escaped chars, strips final space. //looks OK and can print out a list of locations but doesn't work in if clause below $location_short[$u] = preg_replace('/(.*)enabled"\>([\w\d\'&#; ]+)( \(.*)/', '$2', $location[$u]); //works, tested, but has string "Cote d'Azur" $location_short[$u] = str_replace("'",'',$location_short[$u]); // Remove apostrophe, so Cote dAzur, just in case, tested //if ((in_array ($test_location[$u], $countries)) && (in_array ($test_location[$v], $countries))) //simple test works if ((in_array ($location_short[$u], $countries)) && (in_array ($location_short[$v], $countries))) // doesn't work {print $location_link[$u] ;} //works when using $test_location above but not with $location_short } ?> Many thanks for any help Hello, I have some questions about dates/time ... I have a site where registered users will receive messages, I want to include the date/time with that message. I am not sure what is best way to do this, so I am recording the GMT hours offset from users input (from receivers), but then I saw that the hours offset from the GMT is different in different seasons of the year, so how do I detect correct local time for the message receiver? Is it something like a database with start-end time of daylight savings hour adjustment or how is this done? Hello I am only new to this site (my first post) and I am also only new to PHP. I have a site designed call http://www.mediareviewzone.com and I am trying to modify it using PHP. What I am trying to do is when a option is selected in the navigation menu then it will get a variable which will be referenced with a SQL database and then repeated regions will be populated with the table data. For example if you select horror genre in the movies menu, horror will be saved into the url and then the repeated regions will only show the horror movies. The code I have so far is shown below. The repeat region works fine, the only problem I am having is the first if else statement. gen is the variable that would contain horror for the example described above. Thanks for your help and time, as I stated I am only a new to PHP and this is probably an easy question. Code: [Select] <?php if (isset($gen)){ $subject_set = mysql_query("Select * FROM movies WHERE genre ={$_GET['gen']}", $connection);} else{ $subject_set = mysql_query("Select * FROM movies", $connection); } if (!$subject_set){ die("Database connection failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)){?> <div class="par_element"> <div class="par_element2"> <h2> <a href=" <?=$subject['link']?>"><?=$subject['title']?> Review</a></h2> <?= $subject['description']?> <a href=" <?=$subject['link']?>"> read more on <?= $subject['title']?>.</a></div> <a href="<?=$subject['link']?>"> <img src=" <?=$subject['picture']?>" alt="<?=$subject['title']?>" name="<?= $subject['title']?>" width="65" height="100" border="0" id="<?= $subject['title']?>" /></a> </div> <?php } ?> I've searched but can't seem to find anything I can apply to this situation. Most of the resources are dedicated to dropdown menu's from the same table. I have two tables 'regions' and 'cities' and I am looking to create a menu that lists all the cities within a region then moves on to do the same for the rest of the regions in that state: State1 Region1 City1 City2 City3 Region2 City1 City2 City3 Region3 Etc My tables: States table: stateID, state Regions table: regionID, region, stateID Cities table: cityID, city, regionID I currently have two pages- select region, then select city... I'd like to consolidate to one step after choosing a state. I know I need a nested loop, (while?), and to count the results somehow to retrigger loop; also not sure how to handle two mySQL querries in the nested loop. I'm having trouble wrapping my mind around it. Thank you. I am trying to get a group by to work but for some reason it will only display one output. Not sure what the problem is. Code: [Select] mpid mpyear mpmonth mpday mptitle 4 2012 3 2 Text stuff here 3 2012 3 1 Day 1 stuff here 2 2012 2 28 Feb stuff here 1 2011 12 27 First post test here So what I am trying to do is sort out per year month ie: 2012 3 Text stuff here Day 1 stuff here 2 Feb stuff here 2011 12 First post test here That is what I eventually want it to look like, but first thing I need to do is get atleast the mptitle to group first. Code: [Select] $query = "SELECT * FROM monsterpost group BY 'mpmonth'"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo $row['mptitle']; echo "<br />"; } ?> Hi, I have database, screenshot attached. I have to print all records of attendance sort by ID and have sum group by ID. How to know or figure out when the ID changes so that i can show the total for that ID. Bare with me please and try to show me where is my mistake! Code: <?Php include('session.php'); include('dbcon.php'); ?> <html> <head> <link rel="stylesheet" type="text/css" href="bootstrap.css"> </head> <body> <div class="container"> <div class="row"> <div class="col m-auto"> <div class="card mt-5"> <table class="table table-bordered"> <tr> <input type="button" onClick="window.print()" value="Print The Report"/> <td>OracleID</td> <td>Name</td> <td>Designation</td> <td>Clocking In Time</td> <td>Clocking Out Time</td> <td>Duration</td> </tr> <?php $isdone = -1; $query = "select * from attendance_records where isdone= '".$isdone."' order by OracleID"; $result = mysqli_query($con, $query); $query2 = "SELECT sec_to_time(SUM(timestampdiff(SECOND, ClockingInDate, ClockingOutDate))) as total from attendance_records group by OracleID"; $result2 = mysqli_query($con, $query2); while ($row = mysqli_fetch_assoc($result)) { $userid = $row['OracleID']; $name = $row['Name']; $des = $row['Des']; $clockingindate = $row['ClockingInDate']; $clockingoutdate = $row['ClockingOutDate']; $duration = $row['Duration']; $t=time(); $curdate = date("d-m-Y",$t); $curTime = date("g:i:s A",$t); ?> <tr> <td><?php echo $userid ?></td> <td><?php echo $name ?></td> <td><?php echo $des ?></td> <td><?php echo $clockingindate ?></td> <td><?php echo $clockingoutdate ?></td> <td><?php echo $duration ?></td> </tr> <?php } while($row2 = mysqli_fetch_assoc($result2)) { $totals= $row2['total']; echo '<td><h5>Total Attendance Time:</h5></td>'; echo '<td>'; echo $totals; echo '</td>'; } echo '<tr>'; echo 'Report created by ', $_SESSION['user_id2'], ' on ', $curdate, ' at ', $curTime; echo '</tr>'; ?> </table> <a href="home.php" width="100%">Click here to go back to Main Menu</a> </div> </div> </div> </div> </body> </html> and screenshot of result is attached. I have the totals but not at the end of each OracleID, it is at the end of the report. Thanks.
Hi all, does anyone know how to perform this, or if it's even possible? I need every 6th row to be a GROUP BY of the following 5 rows, they all share a common group_id so that's what the GROUP BY is performed on. 1. GROUP BY group_id 2. item #1 3. item #2 4. item #3 5. item #4 6. item #5 7. GROUP BY group_id 8. item #6 ... and so on This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=353968.0 Hi All, I hope i'm posting in the right place but also hope someone can help! I've got the following code: <?php $variations = $product->get_available_variations(); if ($variations): ?> <div class="table-responsive"> <table class="table table-striped"> <tbody> <?php foreach ($variations as $key => $value) : ?> <tr> <td><?php echo $value['sku']; ?> <?php echo $value['variation_id']; ?></td> <?php foreach ($value['attributes'] as $attrKey => $attr) : $tax = str_replace('attribute_', '', $attrKey); $term_obj = get_term_by('slug', $attr, $tax); ?> <td><?php echo $term_obj->name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; #$variations?> </tbody> </table> </div><!-- /table-responsive --> <?php endif; #$variations ?> This produces the following table: Product code System Pack Quantity Variation XT1ECWH 234 System 1 2 N/A XT2ECLWH 236 System 2 2 Left XT2ECRWH 237 System 2 2 Right XT3ECWH 238 System 3 2 N/AHowever, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1XT1ECWH 234 Pack Quantity: 2 Variation: N/A System 2 XT2ECLWH 236 Pack Quantity: 2 Variation: Left XT2ECRWH 237 Pack Quantity: 2 Variation: Right System 3 Sorry, not sure why it's not formatted correctly but hopefully you can see what I'm trying to achieve. The array for $variations is as follows: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [sku] => XT1ECWH [variation_description] => [variation_id] => 234 ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => left ) [sku] => XT2ECLWH [variation_description] => Left [variation_id] => 236 ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => right ) [sku] => XT2ECRWH [variation_description] => Right [variation_id] => 237 ) [3] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [price_html] => [sku] => XT3ECWH [variation_description] => ) ) If it helps, I need to always group by the first attribute, in this case : attribute_pa_system Any help on this would be very much appreciated! Thank you in advanced. Hi
I am reading in data from a csv file
if (($handle = fopen("data.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "User Name: $data[0]"; echo "Booking IDs: $data[1]"; } echo "<hr>";The problem is some of the usernames are duplicated so get outputted the same user name 20 times but with different IDs, obviously but I am unsure how to group them together. I can't seem to figure out how to write if the next username is the same as current then just add another ID not go through the entire loop. Any ideas? Thanks im trying to pull the information for a post which is to include 3 db tables(posts, users, attachments) Ive tried the following code: Code: [Select] $query = $link->query("SELECT p.*, u.u_username, u.u_posts, u.u_avatar, u.u_signature, u.u_avatar_cropped, group_concat(a.a_name, a.a_size, a.a_date_posted separator '<br />') attachments FROM ".TBL_PREFIX."posts as p JOIN ".TBL_PREFIX."users as u ON (u.u_username = p.p_poster) LEFT JOIN ".TBL_PREFIX."post_attachments as a USING (".TBL_PREFIX."posts.p_pid) WHERE p.p_tid = '$tid' GROUP BY p.p_pid ORDER BY p.p_time_posted ASC")or die(print_link_error()); but that generates the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.p_pid) WHERE p.p_tid = '158' GROUP BY p.p_pid ORDER BY p.p_time_pos' at line 10" All i am trying to do is pull everything from posts, join users and select all attachments that belong to the post. Here is my database setup(shortened) asf_posts p_pid | p_name | p_poster 1 test doddsey65 asf_users u_uid | u_username 1 doddsey65 asf_attachments a_aid | a_pid | a_name 1 1 name can anyone help? G'day to all, I need some help building a site that has the ability to accept members and deposit those members in a database. When the members join they will have the ability to add features to their profile; like, skills and certificates. Upload pictures of certificates and a profile pic. The admin area needs to have the ability to add membership types and give those members specific abilities. The admin also needs to have the ability to create profile options, like additional skills. Like a type 1 member has the ability search and see all type 2 members who have a particular skill set. type 2 members just get to see their own profile and the tasks the type 1 members list.
This all might sound like a bunch of brambles but please let me know if you are interested and we can get more involved. Thank you so very much for your help in advanced... Juan Hi Just a quick one. I want my output result to look like this. In its simplistic form I have 2 columns, NAME and NUMBER. Adam 1 2 3 Becky 1 2 3 Charlie 1 2 3 So the output results are grouped by the "name" field, then ordered by the "number" field. I thought it was GROUP BY, but that function is something else. Iv tried searching around but im unsure what you call this form of grouping results. Many Thanks! Hi, Cut down code SELECT count(number) FROM table GROUP BY title echo $title .'<br>'. $number; That outputs Code: [Select] Title1 5 Title2 9 Title3 2 But what do i do if i wanted to retain and loop through the "$number" actual values, not JUST count the total? I still need access to what "$number" is while looping. But as GROUP BY returns one result.. im stuck. Ideally i want this as the resulting output Code: [Select] title1 // grouped title 3 // total number of $numbers per grouped title 1234 12345 123456 // The actual values of $number totalling to the above grouped number title2 // grouped title 2 // total number of $numbers 1234 123456 // The actual values of $number totalling to the above grouped number This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306114.0 Hi all, I have a script that is essentially a log file of all preious versions of an article (versioning) You can see the script I am talking about in action at: http://danielrhyshardy.com/AWT/forumadmin.php What I would like to do is, instead of displaying the title of each instance (you will see there are two instances named "apple", as they are two previous versions of an article), I would like to display that title once, and then all of the instances of that title be displayed below. I hope that makes sense. Here is the code Code: [Select] <? $sql = "SELECT id,title,message,date_added FROM messages2 ORDER BY id "; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$title,$message,$date_added)=mysql_fetch_row($result)){ echo '<div style="color:#bb0000;width:250px;text-align:center;float:left;margin-left:15px;margin-bottom:15px;padding-bottom:20px;"><b>'.$title.'<br>'.$date_added.'</b> <input id="'.$message.'" type="radio" name="admin[]" value="'.$id.'" onclick="javascript:document.form22.car.value=this.id" > </font></div>'."\n"; } ?> I would use something like Code: [Select] if ($title = "apple") { but this would obviously not work for new posts, unless I wanted to constantly update my code. I am sure there is a simple way to achieve this. Perhaps someone can tell me!? Thanks in advance Dan |