TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-14-2010, 08:50 PM   #1 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Bricks 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...

__________________
Downloadic
infolizer

Last edited by CΛSTΞX : 01-16-2010 at 09:58 PM.
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-14-2010, 10:28 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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;
}
delayedinsanity is offline  
Reply With Quote
Old 01-14-2010, 10:30 PM   #3 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

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.

__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-14-2010, 10:36 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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!
delayedinsanity is offline  
Reply With Quote
Old 01-15-2010, 04:42 AM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Old 01-15-2010, 02:23 PM   #6 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Because I want to use it in another file. In another file, I include this one and than I echo the mysql results.
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-15-2010, 06:49 PM   #7 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Anyway to do this using function ?
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-15-2010, 07:43 PM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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.
delayedinsanity is offline  
Reply With Quote
Old 01-15-2010, 08:30 PM   #9 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

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;

                                } 
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-15-2010, 08:36 PM   #10 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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 />';
}
delayedinsanity is offline  
Reply With Quote
Old 01-15-2010, 08:40 PM   #11 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

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...
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-15-2010, 08:44 PM   #12 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

http://www.php.net/manual/en/functions.user-defined.php
delayedinsanity is offline  
Reply With Quote
Old 01-15-2010, 08:45 PM   #13 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Thanks anyway...
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 01-15-2010, 09:20 PM   #14 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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.
delayedinsanity is offline  
Reply With Quote
Old 01-15-2010, 10:24 PM   #15 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Answer Then PHP Question codefreek The Lounge 14 07-20-2009 07:19 PM
question about ftp kritikal General 3 04-28-2008 03:21 PM
$_SERVER['REQUEST_URI'] question solistus General 4 04-01-2008 09:31 PM
Cleaning data before entering database question Killswitch General 7 12-24-2007 11:29 PM
Important Database Structure Question! AnthonyOS MySQL & Databases 5 12-20-2007 03:26 PM


All times are GMT. The time now is 10:44 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design