Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 02:07:00 AM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155476 Posts in 21708 Topics by 7734 Members
Latest Member: rofroodoOvego
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Module Code Issue  (Read 708 times)
mahalleday

Offline Offline

Posts: 188



WWW
« on: December 31, 2008, 01:47:02 AM »

I'm not sure if this is where is should post this or not but since it relates to the creation of a module it should apply.

As some of you know I am working on creating a basic classified ads module for WB.  While porting my code over into a module I am running across some issues.  I am having troubpe creating an array out of the data pulled form the database and then looping thourgh it to display what I want to display.  Belwo is the code I am using.

Code:
<?php
$query 
$database->query($some_sql_statement);
$i=0;
while(
$cel mysql_fetch_array($queryMYSQL_ASSOC) {
        echo 
'<p>' $cel['Row_Title'] . '</p>';
        
$i++;
}
?>


I will admit that I am not as familiar with the available function in WB and how they work as they should be but from what I have read and the code I have seen from working modules this should work fine. 

Any help I can get is greatly appreciated.  All my code is wokring I just need to port it over into a WB mod.
Logged

AdBaker2 Project Site: http://code.google.com/p/adbaker/
marathoner

Offline Offline

Posts: 495


« Reply #1 on: December 31, 2008, 03:22:11 AM »

Wouldn't it just be:
Code:
$query = $some_sql_statement;

Also, why set and increment $i if it isn't used (I know this isn't the problem but general housekeeping).

Something like this
Code:
<?php
$query 
$some_sql_statement;
while(
$cel mysql_fetch_array($queryMYSQL_ASSOC) {
        echo 
'<p>' $cel['Row_Title'] . '</p>';
}
?>

Sorry, not tested
Logged
BerndJM

Offline Offline

Posts: 1764



« Reply #2 on: December 31, 2008, 03:53:06 AM »

Sorry marathoner,

but the syntax mahalleday posted seems absolutely ok - only the $i is a little bit "obscure"

@mahalleday :
you "have trouble to creating the array" -> could you please define this a little bit more.
What happens any error message? a wrong output? ...?

And have you included the "class.database.php"?
and generated a new database object ($database = new database()Wink?

Regards Bernd
« Last Edit: December 31, 2008, 03:59:42 AM by BerndJM » Logged

In theory, there is no difference between theory and practice. But, in practice, there is.
Ralf (Berlin)

Offline Offline

Posts: 1314


« Reply #3 on: December 31, 2008, 05:20:23 AM »

Hello Mahalleday,

perhaps you may have a look at dbConnect for easy creation and access to your database?

Regards
Ralf
Logged
mahalleday

Offline Offline

Posts: 188



WWW
« Reply #4 on: December 31, 2008, 08:06:53 AM »

Thanks for the responses guys.  The '$i' shouldn't be there it is left over from a version where I wasn'y using MYSQL_ASSOC in the mysql_fetch_array() call.

The output I am getting is nothing, the whiel loop does not exectue and all i get is a empty page.  I am trying to atuopulate a form's drop down box, with some categories.  So I want to create an array with the category names from a category table in my database then loop through them to creat the drop down somethign like this...


Code:
<?php>
$query $database->query(some mysql statement);
while (
$var mysql_fecth_array($queryMYSQL_ASSOC) {
echo 
'<select>
<option>'
.$var['row_name'].'</option>
</select>'
;
}
<
?>


This worls just fone if i use my own database connection functions.  I'm trying to use all the built in WB functions in porting over the code into a module though.

Is there a way to turn on errors so I can see whats happening???
ine This works fr
Logged

AdBaker2 Project Site: http://code.google.com/p/adbaker/
aldus

Offline Offline

Posts: 1238


« Reply #5 on: December 31, 2008, 10:21:54 AM »

Quote
Is there a way to turn on errors so I can see whats happening?

You can set the error-reporting-level on "E_ALL" before executing the code,
e.g.
Code:
<?php

$debug 
true;

if (
true === $debug) {
    
ini_set('display_errors'1);
    
error_reporting(E_ALL);
}

echo 
"<select>\n";

$query_result $database->query("some mysql statement");
while (
$var $query_result->fetchRow() ) echo "<option>".$var['row_name']."</option>\n";

echo 
"</select>\n";

?>


Regards
Aldus
Logged
Ruud
WebsiteBaker Org e.V.

Offline Offline

Posts: 2295



WWW
« Reply #6 on: December 31, 2008, 01:30:32 PM »

Code:
<?php>
$query $database->query(some mysql statement);
while (
$var mysql_fecth_array($queryMYSQL_ASSOC) {
echo 
'<select>
<option>'
.$var['row_name'].'</option>
</select>'
;
}
<
?>


possible problems:
1. If you copy/pasted this code, your "while" line is missing a closing )
2. You should not do the <select> within the while loop.

Furthermore you can simply test if your query had a result, and act accordingly:

Code: (untested example)
<?php

$query 
$database->query(some mysql statement);
if(
$query->numRows() > 0) {
    echo 
'<select>';
    while (
$var mysql_fecth_array($queryMYSQL_ASSOC)) {
        echo 
'<option>'.$var['row_name'].'</option>';
    }
    echo 
'</select>';
} else {
    echo 
'No results';
}
?>


Finally: getting no results can also mean your query is not formed correctly.

Ruud

Logged

Professional WebsiteBaker Solutions
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!