Reading rows into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Reading rows into an array

Post by tristanlee85 »

I'm using phpBB and trying to add some features to my forum. The table I am trying to read from is 'phpbb_auction_offer' and the field I would like to create the array from is 'auction_offer_title'. Bascially, 'auction_offer_title' is the title of the different auction entries.

What I am wanting to do is create an array that will read the field from 'auction_offer_title' and store it so it can be echo'd. Here is what I have so far:

Code: Select all

//set array variable
$results = array();

//talk to the db
$query = "SELECT * FROM `phpbb_auction_offer`";
$result = mysql_query($query) or die(mysql_error());

//count the rows and fields
$totalRows = mysql_num_rows($result);
$totalFields = mysql_num_fields($result);

//start the loop
for ( $i = 0; $i < $totalRows; ++$i ) {

//make it 2 dim in case you change your order
  $results[$i] = mysql_fetch_array($result);
This is where I'm stuck. Let's say there are 3 auctions currently in the system and they are titled "dog", "cat", and "mouse." I want to be able call up a variable that I name "current_auctions" and it'll display:

Current auctions: dog, cat, mouse

If there are only "dog" and "cat" in the database, then obviously only display those 2. I've got it figured out where it'll read the first row and display it using this code:

Code: Select all

//
//Count the number of auctions
//

//select the table
$query="SELECT * FROM `phpbb_auction_offer`";
$result=mysql_query($query);

//cound the number of rows to be used later maybe
$num=mysql_numrows($result);

/gets the title of the first auction
$auction_title=mysql_result($result,$i,"auction_offer_title");

//End auction count
And this is the variable I created that I call in the phpBB template:

Code: Select all

'TOTAL_AUCTIONS' => $auction_title,
Basically, I'm stuck on the array part. Can anyone help?
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

This is how I generate an array when I want to display something the main page of my website, using information for my phpBB forums. I'm not sure that it will work with what you're trying to do.

No harm in trying though:

Code: Select all

// query database
	$query = mysql_query ("SELECT * FROM 'phpbb_auction_offer'");


//generate the array
	while ( $result = mysql_fetch_array ($query)){

//echo result
		echo ( $result[auction_offer_title]. ', ' );
	}

The only problem with this code is that it will put a comma after each record, so you need to check for the last record and put a full stop after it. This should work

Code: Select all

// query database
	$query = mysql_query ("SELECT * FROM 'phpbb_auction_offer'");

//count no. of auctions
	$no_auctions = mysql_query ("SELECT COUNT(auction_offer_title) FROM phpbb_auction_offer");

	$current_record = "0";

//generate the array
	while ( $result = mysql_fetch_array ($query)){

//increase current record count
		$current_record++;

//check for last record
		if($current_record==$no_auction){
//if last record, full stop
			echo ( $result[auction_offer_title]. '.' );
		}else{
//if not last record, comma
			echo ( $result[auction_offer_title]. ', ' );
		}
	}
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

Sorry, I just thought, if you want it to be sent somewhere, I am guessing it will be better set out like this:

Code: Select all

// query database
    $query = mysql_query ("SELECT * FROM 'phpbb_auction_offer'");

//count no. of auctions
    $no_auctions = mysql_query ("SELECT COUNT(auction_offer_title) FROM phpbb_auction_offer");

    $current_record = "0";

     $auction_title = "<b>Current auctions: </b>";

//generate the array
    while ( $result = mysql_fetch_array ($query)){

//increase current record count
        $current_record++;

//check for last record
        if($current_record==$no_auction){
//if last record, full stop
            $auction_title .=  $result[auction_offer_title] . '.';
        }else{
//if not last record, comma
            $auction_title .=  $result[auction_offer_title] . ', ' ;
        }
    }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$query = mysql_query('SELECT `auction_offer_title` FROM `phpbb_auction_offer`') or die(mysql_error());

$auctions = array();
while($row = mysql_fetch_row($query))
{
  $auctions[] = array_shift($row);
}

var_dump($auctions);
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

I tried everything, but with no success. Here how my code currently is:

Code: Select all

// query database
    $query = mysql_query ("SELECT * FROM 'phpbb_auction_offer'");

//count no. of auctions
    $no_auctions = mysql_query ("SELECT COUNT(auction_offer_title) FROM phpbb_auction_offer");

    $current_record = "0";

//generate the array
    while ( $result = mysql_fetch_array ($query)){

//increase current record count
        $current_record++;

//check for last record
        if($current_record==$no_auction){
//if last record, full stop
            echo ( $result[auction_offer_title]. '.' );
        }else{
//if not last record, comma
            echo ( $result[auction_offer_title]. ', ' );
        }
    }

$template->assign_vars(array(
	'TOTAL_AUCTIONS' => $result,
Post Reply