Jump to content

separating data after query?


acctman

Recommended Posts

I'm having trouble trying to separate the output into variables that I can use/echo on my page. when I do a print I see the two rows of data all grouped together how can I separate each result base on field and row? maybe something like $line['m_id'][0], $line['m_name'][0], $line['m_id'][1], $line['m_name'][1], etc...

 

 

$bio = mysql_query("SELECT * FROM soc_meminfo 
            WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'");
                             
if (mysql_num_rows($bio) == 0) call404();
while ($line = mysql_fetch_assoc($bio)) {
foreach ($line as $key => $value) {
        $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
        }
echo '<pre>';
    print_r($line);
echo '</pre>';
}

Link to comment
Share on other sites

why wouldn't you simply access the db values via the assoc array that you already have available?

 

$field_name = $line['field_name'];

 

yes i tried that $line['m_id'] $line['m_pos'] etc... that works but it does not separate between rows. there is 1 -2 rows of data coming back from the table. so if i did $line['m_id'] i would get two of them and I won't be able to tell which is which.

Link to comment
Share on other sites

Or just keep it in a numerical index based array and use a foreach loop later:

 

$bio = mysql_query("SELECT * FROM soc_meminfo 
            WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'");
                             
if (mysql_num_rows($bio) == 0) call404();

$lines = array();
while ($line = mysql_fetch_assoc($bio)) {
    $lines[] = $line;
}

// Then later on:
foreach ($lines as $line) {
     echo nl2br($line['field_name']) . PHP_EOL;
     echo nl2br($line['field_name2']) . PHP_EOL;
     // or inner loop as well:
     foreach ($line as $val) {
         echo nl2br($val) . PHP_EOL;
    }
}

 

Then it is one loop, and no mucking about with creating your own design.

 

Edit: added the nl2br, it does the same thing as your str_replace, but with the proper method for it.

Link to comment
Share on other sites

this is confusing me a bit, I have an idea... is there a way to add an increment number to $line for every complete loop? so then i'll have $en['bm_pos0'] and $en['bm_pos1'] this will allow me to use <%bm_pos0%> and <%bm_pos1%> in my template coding. So how can I add just a number after each complete loop (row of fields)

 

while ($line = mysql_fetch_assoc($bio)) {   
foreach ($line as $key => $value) {
    $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
    }
}

Link to comment
Share on other sites

something like this i'm trying to do. I think my order is wrong for the increment

 

if (mysql_num_rows($bio) == 0) call404();
$i=1; 
while ($line = mysql_fetch_assoc($bio)) {   
$line . $i;
    foreach ($line as $key => $value) {
        $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
        }
        $i++;
    }
echo $en['bm_pos1'];

Link to comment
Share on other sites

i figured it out...

 


$i = 1;
if (mysql_num_rows($bio) == 0) call404();
    while ($line = mysql_fetch_assoc($bio)) { 
        foreach ($line as $key => $value) {
        $en['b'.$key . $i] = str_replace("\n",'<br/>',stripslashes($value));
        }
    $i++;        
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.