<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1032813" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1032813</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/phpmysql-issue#comment-1191925</link>
    <description> &lt;p&gt;And you can take Greg&#039;s example one step further and get some more control over the order of your results:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;$query &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= \&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;SELECT name, vehicle FROM cars GROUP BY vehicle ORDER BY name, vehicle ASC\&quot;;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = mysql_query(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = \&quot;\&quot;;&lt;br /&gt;&lt;br /&gt;while (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = mysql_fetch_assoc(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;))&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;] == \&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;\&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo \&quot;|\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; != \&quot;\&quot;) echo \&quot;&amp;lt;br /&amp;gt;\n\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;] . \&quot;\t\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;vehicle&#039;];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;echo \&quot;&amp;lt;br /&amp;gt;\n\&quot;;&lt;br /&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;I&#039;ve used mySQL&#039;s &quot;GROUP BY&quot; and &quot;ORDER BY&quot; to achieve this.&lt;/p&gt;
&lt;p&gt;Edit: fixed a small error in the code.&lt;/p&gt;
 </description>
     <pubDate>Sun, 29 Jan 2006 02:33:06 +0000</pubDate>
 <dc:creator>nuk3</dc:creator>
 <guid isPermaLink="false">comment 1191925 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/phpmysql-issue#comment-1191889</link>
    <description> &lt;p&gt;The below should work for you. &lt;/p&gt;
&lt;p&gt;First note that the query was changed to sort by the &quot;NAME&quot; as all rows with the same &quot;name&quot; have to come together. For this to work, you are basically checking to see if the &quot;name&quot; of the current row is the same as the last row (saved in the variable $lastname).&lt;/p&gt;
&lt;p&gt;It is good programing practice to initialize any variable before comparing it, so we set it to be empty before starting the loop.&lt;/p&gt;
&lt;p&gt;Also note, to make it easier to read the code, I used the FETCH_ASSOC instead of FETCH_ROW, so that the array has the data&#039;s keys matching the field names. Again this is good practice, as as you get to longer code, and more fields returned, this is easier to deal with $row[&#039;password&#039;] instead of $row[8] especially when you are so many lines down that the original query is not viewable in your editor.&lt;/p&gt;
&lt;p&gt;Now on to the loop.&lt;/p&gt;
&lt;p&gt;The first thing we do is compare this rows name with the name from the last row (or nothing for the first row).&lt;/p&gt;
&lt;p&gt;If it is the same person, we only need to output the | character. If it is not the same, we output a line break followed by the new name and the tab. Note, I included the extra if statement there so the line break is not done before the first name.&lt;/p&gt;
&lt;p&gt;After all that, it outputs the vehilce name. Note, we do not put the line break here, as if the next record is the same person, we need it to be on the same line.&lt;/p&gt;
&lt;p&gt;Lastly, we set the variable $lastname to be this records name to compare with on the next row.&lt;/p&gt;
&lt;p&gt;Oh yeah, I gues really the last thing is after the while loop, we need to send out one final line break.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;-Greg&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;$query &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= \&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;SELECT name, vehicle FROM table ORDER BY name \&quot;;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = mysql_query(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = \&quot;\&quot;;&lt;br /&gt;&lt;br /&gt;while (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = mysql_fetch_assoc(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;))&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;]==&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo \&quot;|\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;!=\&quot;\&quot;) echo \&quot;\n\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;] . \&quot;\t\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;vehicle&#039;];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$lastname&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$line&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&#039;name&#039;];&lt;br /&gt;}&lt;br /&gt;echo \&quot;\n\&quot;;&lt;br /&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/p&gt;
 </description>
     <pubDate>Sat, 28 Jan 2006 18:44:14 +0000</pubDate>
 <dc:creator>Greg K</dc:creator>
 <guid isPermaLink="false">comment 1191889 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
