Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i show multiple data in one string in php and mysql

i have 2 tables in my database one called topics and the other called sections

every topics has multiple sections and they linked to main topic by id

for example this is my datable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

topics

id
title
content

sections

id
title
topic_id

i use this code to show content

<?php
$show_topics = $topics->select_topics($page);
while ($rows_topics = mysqli_fetch_array($show_topics [0])) {
    $id_topic       = $rows_topics['id'];
    $title_topic    = $rows_topics['title'];
    $content_topic    = $rows_topics['content'];

    echo $content_topic;

    $select_sections = $topics->view_section_limit($id_topic, 0, 100);

    while ($rows_sections = mysqli_fetch_array($select_sections)) {
        $id_section           = $rows_sections['id'];
        $section_title        = $rows_sections['title'];
        echo $section_title;
    }
}
?>

i want to combine all content in one string, i used this code for that

<?php
$show_topics = $topics->select_topics($page);
while ($rows_topics = mysqli_fetch_array($show_topics [0])) {
    $id_topic       = $rows_topics['id'];
    $title_topic    = $rows_topics['title'];
    $content_topic    = $rows_topics['content'];
        
    $select_sections = $topics->view_section_limit($id_topic, 0, 100);
    
    while ($rows_sections = mysqli_fetch_array($select_sections)) {
        $id_section           = $rows_sections['id'];
        $section_title        = $rows_sections['title'];
        
        $all_content = $content_topic." ".$section_title;
        echo $all_content;
    }
}
?>

but it’s duplicate content_topic With all section_title

i want to do that becuse i want to add all section titles to topic contents like that

$edit_content = $topics->edit_topic_content($all_content, $id_topic);

for example if i have this topics_content

topics_content = "this is a beautiful day"

and i have 3 sections titles like

section 1 title = "camera 1"
section 2 title = "camera 2"
section 3 title = "camera 3" 

the final data shows like this

this is a beautiful day camera 1 this is a beautiful day camera 2 this
is a beautiful day camera 3

i want to make it like this

this is a beautiful day camera 1 camera 2 camera 3

>Solution :

You need to make a new variable, and append the $content_topic to it only once, during the outer loop, then just add the section titles within the inner loop.

Something like this:

<?php
$show_topics = $topics->select_topics($page);

while ($rows_topics = mysqli_fetch_array($show_topics [0]))
{
    $id_topic       = $rows_topics['id'];
    $title_topic    = $rows_topics['title'];
    $content_topic    = $rows_topics['content'];
        
    $select_sections = $topics->view_section_limit($id_topic, 0, 100);

    $all_content = $content_topic;
    
    while ($rows_sections = mysqli_fetch_array($select_sections)) {
        $id_section           = $rows_sections['id'];
        $section_title        = $rows_sections['title'];
        
        $all_content .= " ".$section_title;
    }

    echo $all_content;
}
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading