Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge multiple database results into one
    primarykey
    data
    text
    <p>I am unsure as to what to call this question. I have been thinking a lot about it, and I do not think that what I've chosen covers it adequately, but it was the best I could manage. Forgive me if it is very misleading or incorrect.</p> <p>I am creating a small CMS for personal use, but I have stumbled upon trouble.</p> <p>The information is stored in a MySQL database, and the table has three columns: <em>cid</em>, <em>page</em> and <em>cont</em>.</p> <p>The cid is the identifier of a certain piece of content. If a <em>page</em> has multiple pieces of content, such as a main area of text and a sub area of text, the <em>cid</em> on these rows would be different, but the <em>page</em> would be the same, seeing as the content is on the same page.</p> <p>On the index page of the CMS I am then displaying all the different pages that exist in the database, as links. The user then clicks the link to the page on which he'd want to change the content.</p> <p><strong>Now, here is my issue:</strong></p> <p>When displaying the data from the database, rows that have the same page, but different <em>cid</em> is, logically, displayed as separate links.</p> <p>That sounded confusing. I'll say it differently:</p> <p><strong>What I need is that</strong> if a result has the same <em>page</em> as another result, these two are merged together into one link, and not displayed as two separate links.</p> <p>This is how the database looks:</p> <pre><code>+------------+----------+-----------------------------------------+ | cid | page | cont | +-----------------------------------------------------------------+ | page1-main | page1 | This is the first page on this website! | | page2-main | page2 | This is the second page! | | page2-sub | page2 | This is sub-content! | +------------+----------+-----------------------------------------+ </code></pre> <p>As I said, what I want is to have any rows that have the same <em>page</em> be merged into one result in my while loop, because what happens now is that they are separated into two different results. This is my loop and query:</p> <pre><code>$query = mysqli_query("SELECT * FROM content"); while($row = mysqli_fetch_array($query)) { ?&gt; &lt;a href="edit.php?p=&lt;?php echo $row['page'];?&gt;"&gt;Link!&lt;/a&gt; &lt;?php } </code></pre> <p>That will, of course, display two results, even though they have the same page. I am unsure as to whether it is some PHP I will have to do, or it is in the MySQL query. I am a good bit unexperienced in SQL, but fairly experienced in PHP.</p> <p>I've been thinking about some ifs. Like, storing every new result that is displayed in a variable and then comparing them, and if one that has occurred before occurs again, then it is not displayed, or something. But I don't know.</p> <p>It would be wonderful if anyone could help me out here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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