-1

here i have a simple function but this show me data fom sql only in 1 div i want to show [ on div 1 show 1 data, in other div show 2 data, etc etc]...

function load_post($added_by)
{
    global $Connection;

    $SQL_3 = mysqli_query($Connection, "SELECT * FROM posts WHERE added_by='$added_by'");   

    $NumPosts = mysqli_num_rows($SQL_3);
    $out['num_posts'] = $NumPosts;

    while($Fetch_3 = mysqli_fetch_array($SQL_3))
    {           
        $out['id'] = $Fetch_3['id'];
        $out['text'] = $Fetch_3['text'];
        $out['added_by'] = $Fetch_3['added_by'];
        $out['mp4'] = $Fetch_3['mp4'];
        $out['likes'] = $Fetch_3['likes'];
        $out['youtube'] = $Fetch_3['youtube'];
        $out['image'] = $Fetch_3['image'];
        $out['date_added'] = $Fetch_3['date_added'];

        return $out;
    }
}

index.php.

$posts = load_post('gentritabazi');
<div class="settings_forms_content">
<?php echo $posts['text']; ?>
</div>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

3 Answers3

0

return finish immediately your function so only one iteration is done. You need to save your results in array and then after loop return that array, sth like this:

$out = array(); 
while($Fetch_3 = mysqli_fetch_array($SQL_3))
        {           
            $out[] = $Fetch_3;


        }
return $out;

and display:

$posts = load_post('gentritabazi');
foreach ($posts as $post) {
 echo '<div class="settings_forms_content">';
            echo $post['text'];
            echo '</div>';
}
nospor
  • 4,190
  • 1
  • 16
  • 25
0
$posts = load_post('gentritabazi');
foreach ($posts ['text'] as $postText){
  echo "<div class='settings_forms_content'>$postText</div>";            
}

first line calls your function which returns the array $posts this array looks like:

$posts = array(
  "id" => array of ids
  "text" => array of texts
  ...
);

if you want to access the third text it would be like this:

$posts ['text'][3]

the foreach iterates to your $post array with index "text" -> which is also an array every value in this array, $post['text'] will be referenced with $postText -> that means: $post['text'][1] = $postText (first time looping through foreach-loop) $post['text'][2] = $postText (secondtime looping through foreach-loop) .. if you are familiar with loops, a foreach is just the short version for

  for(var $i=0;$i<length($posts['text'];$i++){
    echo "<div class='settings_forms_content'>$posts['text'][i]</div>";   
  } 
njank
  • 319
  • 3
  • 12
  • 2
    Add more details explaining your answer. Code only is not sufficient. – Bassem May 30 '16 at 22:19
  • While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Box Box Box Box May 31 '16 at 02:52
  • added explanation to my comment – njank May 31 '16 at 16:04
0

Lots to amend, so I commented the code rather than write an essay

function load_post($con, $added_by)
{
    // dont use globals, use parameters
    //global $Connection;

    // select only what you want to see not `*`
    $SQL_3 = mysqli_query($con, "SELECT id, text, added_by, mp4,
                                        likes, youtube, image, data_added
                                 FROM posts 
                                 WHERE added_by='$added_by'");   

    // not needed
    //$NumPosts = mysqli_num_rows($SQL_3);
    //$out['num_posts'] = $NumPosts;

    while($Fetch_3 = mysqli_fetch_array($SQL_3))
    {       
    /*
     * THsi code would overwrite the last iteration of the while loop   
        $out['id'] = $Fetch_3['id'];
        $out['text'] = $Fetch_3['text'];
        $out['added_by'] = $Fetch_3['added_by'];
        $out['mp4'] = $Fetch_3['mp4'];
        $out['likes'] = $Fetch_3['likes'];
        $out['youtube'] = $Fetch_3['youtube'];
        $out['image'] = $Fetch_3['image'];
        $out['date_added'] = $Fetch_3['date_added'];
    */
        // now as you SELECT only what you want yo can simply do

        $out[] = $Fetch_3;

        //return $out; instantly terminates the function
    }

    return $out;   // return the array
}

Now call your function

// pass the connection as a parameter
$posts = load_post($Connection, 'gentritabazi');

// if you want to know how many results were returned
$result_count = count($posts);

// process the returned array
foreach ( $posts as $post ) {

    echo '<div class="settings_forms_content">';
    echo $post['text'];
    echo '</div>';
}

Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared statement and parameterized statements

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149