Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I convert this MySQL Data Into This XML String (PHP)?
    text
    copied!<p>On one of my websites, I am logging all the visitors and their country codes in a MySQL table.</p> <p>For simplicity, let's just say the columns of this table are "IP" and "Country". So I have a big table of visitors from several different countries. </p> <p>For display purposes, I need to put this data into an XML format string like this:</p> <pre><code>&lt;set label='US' value='17'/&gt; &lt;set label='FR' value='12'/&gt; &lt;set label='GB' value='18'/&gt; &lt;set label='AU' value='8' /&gt; </code></pre> <p>Where <strong>value='' is how many visitors</strong> are from that country. I want the string to include ALL the countries that are in the table.</p> <p>One way I thought I could do this was to count all the rows with the certain country, for example:</p> <pre><code>$query = mysql_query("SELECT * FROM visitors WHERE country='US'"); $string.="&lt;set label='US' value='".mysql_num_rows($query)."'/&gt;"; $query = mysql_query("SELECT * FROM visitors WHERE country='FR'"); $string.="&lt;set label='FR' value='".mysql_num_rows($query)."'/&gt;"; $query = mysql_query("SELECT * FROM visitors WHERE country='DE'"); $string.="&lt;set label='DE' value='".mysql_num_rows($query)."'/&gt;"; </code></pre> <p>The problem, I quickly realized, was that my visitors are from hundreds different countries and this is really messy! Besides, I might miss a country that someone comes from in the future.</p> <p>Isn't there an easier way to do this? Maybe with a special array and foreach or something...</p> <p>Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload