TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   While Question (http://www.talkphp.com/general/5222-while-question.html)

CΛSTΞX 01-14-2010 08:50 PM

While Question
 
Hello, my script is using while to get mysql search results. Here is that part:

PHP Code:

while($row=@mysql_fetch_array($nt)){

$results $row['title'];

echo 
$results;



I want to give a value for results, and than echo it. I mean, I dont want to echo this in while section. Anyway to do this?
Thanks...


delayedinsanity 01-14-2010 10:28 PM

Not sure if I'm following you?

php Code:
$results = array();

while ( $row = mysql_fetch_array( $nt ) ) {
    $results[] = $row['title'];
}

print_r( $results );

foreach ( $results as $title ) {
    echo $title;
}

CΛSTΞX 01-14-2010 10:30 PM

Thanks, I found this one which is very similar with yours, too.

PHP Code:

$results = array();
while(
$row=@mysql_fetch_array($nt)){
    
$results[] = $row;
}


foreach(
$results as $key=>$val) {
   echo 
$val['title']; //Print all found titles.



delayedinsanity 01-14-2010 10:36 PM

Exactly the same thing, I just didn't know if you wanted to grab the entire row or just the title. ;) If you are just grabbing the title, make sure your SQL query reflects as such!

Village Idiot 01-15-2010 04:42 AM

Is there a reason you are storing the data then directly outputting it? I can't see a real reason to do that.

CΛSTΞX 01-15-2010 02:23 PM

Because I want to use it in another file. In another file, I include this one and than I echo the mysql results.

CΛSTΞX 01-15-2010 06:49 PM

Anyway to do this using function ?

delayedinsanity 01-15-2010 07:43 PM

Lots of different ways, it all depends on what you want the data to look like and what you plan on doing with it. Without knowing the purpose of the query/display, only you know the best way, all we can do is provide examples of methods to use.

php Code:
function data_dump( $query ) {
    $results = mysql_query( $query );

    $data = array();

    while ( $row = mysql_fetch_assoc( $results ) )
        $data[] = $row;

    return $data;
}

VI has a point though, if ALL you are doing is directly echo'ing the data to STDOUT, there's no reason to take up resources storing it. Just echo it. If you need to perform operations on the data first, then echo it, store it in an array.

CΛSTΞX 01-15-2010 08:30 PM

Thanks, but I can't make it a function like you said, can you help me ?

PHP Code:

$results = array();
while(
$row=@mysql_fetch_array($nt)){
    
$results[] = $row;
}


foreach(
$results as $key=>$val) {

$downloadiclink "<h3><a href='http://www.downloadic.com/$val[id]-$val[alt_name].html'>$val[title]</a></strong></h3>$val[short_story]<br>";

echo 
$downloadiclink;

                                } 


delayedinsanity 01-15-2010 08:36 PM

I don't think we're making any headway here. You're making the whole process a lot more difficult than it needs to be, and that's probably partially my fault for the original example.

php Code:
// Why are you suppressing errors here? Shouldn't be a need for that.
while ( $row = mysql_fetch_array( $nt ) ) {
         echo '<h3><a href="http://www.downloadic.com/' . $row['id'] . '-' . $row['alt_name'] . '.html">' . $row['title'] . '</a></h3>' . $row['short_story'] . '<br />';
}

CΛSTΞX 01-15-2010 08:40 PM

I have a file which is named mysql.php, I want to make a function in this file and than in another file which is search.php, I include mysql.php and than use the function to get mysql results. Thanks...

delayedinsanity 01-15-2010 08:44 PM

http://www.php.net/manual/en/functions.user-defined.php

CΛSTΞX 01-15-2010 08:45 PM

Thanks anyway...

delayedinsanity 01-15-2010 09:20 PM

I'm not sure how to help you from here without writing it for you - any block of code can be turned into a function to do this. If you're including the file, it doesn't necessarily even need to be a function, unless of course it's a library of functions meant for a variety of purposes, which is usually the concept behind having a seperate file inclusion.

The above link illustrates how to form a user defined function. You place the code inside the function, include the file, and call the function.

Village Idiot 01-15-2010 10:24 PM

Not knowing how to do something is quite usual for a programmer, it is a needed trait of any good programmer to be able to work from what they have and get the job done. Break what you need to do down and work it out from there. Google is your friend for each little step. If you can't do that, there is no real help that we can give you. Sorry if this comes across as insulting, but you do not appear to even be attempting to think on your feet here.


All times are GMT. The time now is 09:55 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0