Re: [PHP] RE: Help with my first recursion menu

2009-11-01 Thread Lex Braun
Hi,

On Sat, Oct 31, 2009 at 11:07 AM, MEM  wrote:

>
>
> *From:* Lex Braun [mailto:lex.br...@gmail.com]
> *Sent:* sábado, 31 de Outubro de 2009 14:05
> *To:* MEM
> *Cc:* php-general@lists.php.net
> *Subject:* Re: [PHP] RE: Help with my first recursion menu
>
>
>
> Hi,
>
> On Wed, Oct 28, 2009 at 5:22 PM, MEM  wrote:
>
> I've been told that stack is the way to go, so I'm trying to understand the
> following code:
> http://pastebin.com/m5616c88f
> I've commented every line so that any of you could see if I'm interpreting
> something wrong:
>
>
> I have two questions about this code, that hopefully someone on the list
> could explain:
>
> 1)
> Why do we need to remove the last array item? (on line 32):
> array_pop($urlStack);
>
> On line 20, $url creates an URL path that includes the $cat path.  Once
> you've completed iteration on all children of $cat, the url path should no
> longer include $cat path, thus it is removed from $urlStack.
>
>
>
>
> 2)
> Why we print the closed tag of the li element, after the recursive call?
> (on
> line 29)
> echo "\n";
>
>  Line 29 is closing the li element that was opened on line 23 for $cat.
>
>
>
> Thanks a lot in advance,
> Márcio
>
>  -Lex
>
>
>
>
>
>
>
>
>
> Thanks a lot. I knew that:
>
> “Line 29 is closing the li element that was opened on line 23 for $cat.”
>
>
>
> My question intend to be more like:
>
> Why that line to close the li element is **after** the recursive call?
>
> Somehow, it seems that instead of creating something like this:
>
> 
>
> item1
>
> item2
>
> item3
>
> 
>
>
>
> We are doing:
>
> 
>
> item1
>
> item2
>
> item3
>
> 
>
> 
>
>
>
Say you have a tree menu that contains :
$arr = array('item1' => 'Item 1', 'item2' => array('item2a' => 'Item 2a',
'item2b' => 'Item 2b'));
$urlStack = array('category');

When you call the tree() function with the above $arr values, the function
acts as follows:
1. First time through the function, the opening ul element is printed
2. First time through the foreach loop sets $cat = 'item1' and $val = 'Item
1'.
3. As this is not an array, the else condition prints Item 1
4. Second time through the foreach loop sets $cat = 'Item 2' and $val =
array('item2a' => 'Item 2a', 'item2b' => 'Item 2b').
5. As this is an array, the if condition prints Item and
calls the tree function recursively
6. Second time through the tree function, another  element is printed
and the two sub items are printed as sub item as they do not
contain additional arrays.  Tree function ends by printing the closing 
tag.
7. The foreach loop from step 5 continues and prints the closing . Tree
function ends by printing the closing  tag.

The output from calling tree($arr, $urlStack) would thus be:

Item 1

Item 2

Item 2a
Item 2b





Re: [PHP] RE: Help with my first recursion menu

2009-10-31 Thread Lex Braun
Hi,

On Wed, Oct 28, 2009 at 5:22 PM, MEM  wrote:

>  I've been told that stack is the way to go, so I'm trying to understand
> the
> following code:
> http://pastebin.com/m5616c88f
> I've commented every line so that any of you could see if I'm interpreting
> something wrong:
>
>
> I have two questions about this code, that hopefully someone on the list
> could explain:
>
> 1)
> Why do we need to remove the last array item? (on line 32):
> array_pop($urlStack);
>
On line 20, $url creates an URL path that includes the $cat path.  Once
you've completed iteration on all children of $cat, the url path should no
longer include $cat path, thus it is removed from $urlStack.


>
> 2)
> Why we print the closed tag of the li element, after the recursive call?
> (on
> line 29)
> echo "\n";
>
Line 29 is closing the li element that was opened on line 23 for $cat.

>
>
> Thanks a lot in advance,
> Márcio
>
-Lex


Re: [PHP] supplied argument errors

2009-06-23 Thread Lex Braun
On Tue, Jun 23, 2009 at 4:10 PM, PJ  wrote:

> I think there is something I do not understand in the manual about
> mysql_fetch_assoc(), mysql_affected_rows()
> The code works, but I get these annoying messages.
> snippet:
>

What are the warnings?


Re: [PHP] Check out system for Shopping Cart

2009-05-21 Thread Lex Braun
Vernon,

CC messages to php-general to keep responses on-list. See my comments
inline.

On Thu, May 21, 2009 at 12:06 PM, Vernon St.Croix wrote:

>
> I have amended as you advised and the mysqli errors are now gone. Thanks
> for that.
>
> I however have another one I can't get rid of:
>
>
> *Warning*: Invalid argument supplied for foreach() in *
> C:\wamp\www\draft\checkout.php* on line *53*
> Your order could not be processed.
>
>
> *checkout.php*
>
> 



>
>
> include ("mysql_connect.php");
>
> //Turn off autocommit
>
>
> $con = mysqli_connect ("localhost", "root", "", "rum");
>

> mysqli_autocommit($con, FALSE);
>
> //Add orders to the order table
>
> $sql = "INSERT INTO orders (user_id, total) VALUES ($user, $total)";
>
> $r = mysqli_query($con, $sql);
>



>
>  *   foreach ($_SESSION['cart'] as $id => $item){*
>
> $qty = $item['quantity'];
> $price = $item['price'];
>
> mysqli_stmt_execute($stmt);
>
> $affected += mysqli_stmt_affected_rows($stmt);
>
> }
>

The reason you're getting this warning is that foreach is expecting
$_SESSION['cart'] to be an array.  Do a print_r() or var_dump() of
$_SESSION['cart'] to see what is actually being passed to the function.

mysqli_stmt_close($stmt);
>
> if ($affected ==count($_SESSION['cart'])) {
>
> mysqli_commit($con);
>
> echo'Thank you for your order';
>
> }else{
>
> mysqli_rollback($con);
>
> echo'Your order could not be processed';
>
> }
>
> }else{
>
> mysqli_rollback($con);
>
> echo'System error';
>
> }
>
> mysqli_close($con);
>
>
>
>
> include ("footer.html");
>
>
> ?>
>
>
> VEE
>

-- Lex


Re: [PHP] Check out system for Shopping Cart

2009-05-21 Thread Lex Braun
On Thu, May 21, 2009 at 7:32 AM, Vernon St.Croix wrote:

> *Warning*: mysqli_query() expects parameter 1 to be mysqli, object given
> in *C:\wamp\www\draft\checkout.php* on line *26*
>
> ***for the script
>
> 
> include("mysql.class.php");
>
> include ("header.php");
>
>
>
> //include ("functions.php");
>
> $user = 1;
> $total = 178.93;
>
> include ("mysql_connect.php");
>
> //Turn off autocommit
>
> //mysql_autocommit($con, FALSE);
>
> //Add orders to the order table
>
> $sql = "INSERT INTO orders (user_id, total) VALUES ($user, $total)";
>
> $r = mysqli_query($con, $sql);*


The warning message is saying that it's expecting the first parameter you
passed in mysqli_query() to be a mysqli link identifier, i.e., that the
variable $con used in the line above be your connection to the database.
Somewhere before this function call, you need to set $con:

$con = *mysqli_connect* ($host, $username, $passwd, $dbname);

-- Lex


Re: [PHP] Non-Object errors

2009-04-28 Thread Lex Braun
Terion,

On Tue, Apr 28, 2009 at 1:14 PM, Miller, Terion <
tmil...@springfi.gannett.com> wrote:

> Here is my code now:
>   $query = "select name, age, warrant, bond, wnumber, crime FROM warrants
> where ".$warranttype." = ".$warranttype." OR ".$searchname." =
> ".$searchname." ";$result = mysql_query($query);$row =
> mysql_fetch_assoc($result);$num_results = mysql_num_rows ($result);
> Should this post of gone on the Db list maybe?
>

One thing I noticed about your code above has to do with your WHERE clause.
Say,
$warranttype = 'warrant'
$searchname = 'Terion'

Your $query will become:
SELECT name, age, warrant, bond, wnumber, crime
FROM warrants
WHERE warrant = warrant OR Terion = Terion

The first time you use $warranttype and $searchname should be the column
names rather than the value you would like that column to be equal to.

-- Lex


Re: [PHP] difficult select problem

2009-04-07 Thread Lex Braun
PJ,

On Tue, Apr 7, 2009 at 11:37 AM, PJ  wrote:

> $SQL = "SELECT b.*, c.publisher, a.first_name, a.last_name
>FROM book AS b
>LEFT JOIN book_publisher as bp ON b.id = bp.bookID
>LEFT JOIN publishers AS c ON bp.publishers_id = c.id
>LEFT JOIN book_author AS ba ON b.id = ba.bookID
>LEFT JOIN author AS a ON ba.authID = a.id
>WHERE LEFT(last_name, 1 ) = '$Auth' ";
>
> (PLEASE LET ME KNOW IF THERE IS SOMETHING WRONG WITH THE CODE)
>

Let me try to clarify what I'm saying about your query.  The above query
will ONLY return authors who match the WHERE condition, thus have last name
starting with A.  This query will never find the second author for those
books, unless that author's last name also starts with an A.  That's why you
first need to get a list of the book IDs that match your WHERE condition,
and then grab the authors related to those book IDs (whether through two
queries or using a sub-query).  You've been shown several different ways of
doing this through the responses provided.  Do the provided queries not work
for your test data?

- Lex


Re: [PHP] difficult select problem

2009-04-06 Thread Lex Braun
On Mon, Apr 6, 2009 at 2:05 PM, PJ  wrote:

> My code already has selected the books whose authors last names start
> with A as well as the authors themselves.
> Within the results some books have 2 authors.


If you already have the book IDs where this happens, then for each bookID
(probably a foreach loop in your code) simply do a query at that point that
will obtain ALL authors for that bookID -- don't include the condition that
last name start with an A in this query.

 > $query = "SELECT b.id , GROUP_CONCAT(CONCAT(' ',
> > a.first_name, a.last_name) ORDER BY a.last_name)
> I believe this should be (CONCAT_WS(' ', a.first_name, a.last_name) or
> (CONCAT(a.first_name,' ', a.last_name)
>
Sorry, yes that should be
GROUP_CONCAT( CONCAT(' ', a.first_name, a.last_name) ORDER BY a.last_name)
so that it concatenates each author as "FirstName LastName" and then groups
all authors for a given book separated by commas, so bookID=1 would have
something like "Author1FirstName LastNameAuthor1", "FirstNameAuthor2
LastNameAuthor2"

Problem here is that I need to use the ba.ordinal somewhere in the cod
> to distinguish between the authors - if ba.ordinal is 1, the author is
> listed as the first author and if there is no ba.ordinal = 2 for the
> ba.bookID, then there is only 1 author. I have not included this in my
> query because it only returns the ordinal number for the author which
> does not help me at all. I need to know who the second author is.
>
> In effect, it would be great if I could get 2 joins on the same table
> (author) for
> 1.  (CONCAT(first_name, ' ', last_name) AS Author WHERE ab.ordinal = 1
> && LEFT(last_name, 1) = $Auth
> 2.  (CONCAT(first_name, ' ', last_name) AS Author1 WHERE ab.ordinal = 2


The problem you'd run into with #1 above, is you'd only get authors whose
last name starts with "A" if they're the primary author. Is that the desired
functionality you're looking for?

- Lex


Re: [PHP] difficult select problem

2009-04-06 Thread Lex Braun
PJ,
On Mon, Apr 6, 2009 at 12:32 PM, PJ  wrote:

> I am trying to limit the search for books to only those that start with
> "A" (does not have to be case sensitive); but within that selection
> there may be a second author whose name may start with any letter of the
> alphabet.
>

First off, are you trying to search for book TITLES that start with "A" or
book AUTHORS that start with "A"?

If it's authors, it might make more sense to do a select that retrieves the
book ids that have an author whose last name starts with an "A" and once you
have those IDs, do a second query that will retrieve all authors for those
books. So something like below (UNTESTED):

//Find out which books are written by an author with a last name starting
with $Auth
$SQL = "SELECT b.id FROM book AS b
LEFT JOIN book_author AS ba ON b.id = ba.bookID
LEFT JOIN author AS a ON ba.authID = a.id
WHERE LEFT(last_name, 1) = '$Auth' ";

Then do the following select statement with a foreach loop that retrieves
the book IDs above

$query = "SELECT b.id, GROUP_CONCAT(CONCAT(' ', a.first_name, a.last_name)
ORDER BY a.last_name)
FROM book AS b
LEFT JOIN book_author AS ba ON b.id = ba.bookID
LEFT JOIN author AS a ON ba.authID = a.id
WHERE b.id = $value
GROUP BY b.id "; // $value is the b.id for each iteration of the foreach
loop

More information about the GROUP_CONCAT() function can be found in the MySQL
reference (
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
)

- Lex


Re: [PHP] & and && and weird results

2009-04-01 Thread Lex Braun
PJ,

On Wed, Apr 1, 2009 at 1:40 PM, PJ  wrote:

> SELECT * FROM book
>WHERE id IN (SELECT bookID
>FROM book_author WHERE authID IN (SELECT author.id
>FROM author WHERE LEFT(last_name, 1 ) = '$Auth' &&
> LEFT(last_name, 1 ) = '$Auth1' && LEFT(last_name, 1 ) = '$Auth2'));
>

Say $Auth = "I", $Auth1 = "J", $Auth2 = "K".
The above sub-query is stating select author ids where the first digit in
last_name is I AND the first digit in last_name is J AND the first digit in
last_name is K.  For any given last_name (thus, author id), the first digit
can only be one of those options, thus this sub-query will return ZERO
results.

To get a sub-query that will only return results that start with I, J,
or K,you can instead use the following sub-query:
SELECT author.id FROM author WHERE LEFT(last_name, 1) = '$Auth' OR
LEFT(last_name, 1) = '$Auth1' OR LEFT(last_name, 1) = '$Auth2'

The above will return all author IDs where the surname starts with I, J, or
K.

- Lex


Re: [PHP] validation & inserts not working

2009-03-11 Thread Lex Braun
On Wed, Mar 11, 2009 at 5:44 PM, PJ  wrote:

> haliphax wrote:
> > On Wed, Mar 11, 2009 at 4:29 PM, PJ  wrote:
> >
> >> Lex Braun wrote:
> >>
> >>> PJ,
> >>>
> >>>
> >>>
> >>>> Â  Â  
> >>>>
> >>>>
> >>>>> Â  Â  $sql1 = "INSERT INTO book ( title, sub_title, descr,
> >>>>> Â  Â  Â  Â  Â  Â  Â  Â comment, bk_cover, copyright, ISBN, language,
> sellers )
> >>>>> Â  Â  Â  Â  Â  Â  VALUES ('$titleIN', '$sub_titleIN', '$descrIN',
> '$commentIN',
> >>>>> Â  Â  Â  Â  Â  Â  Â  Â '$bk_coverIN', '$copyrightIN', '$ISBNIN',
> '$languageIN',
> >>>>> Â  Â  Â  Â  Â  Â  Â  Â '$sellersIN')";
> >>>>> Â  Â  Â  Â $result1 = mysql_query($sql1, $db);
> >>>>> Â  Â  Â  Â $autoid = mysql_insert_id($result1);
> >>>>>
> >>>>>
> >>> You're actually sending mysql_insert_id() the wrong parameter. It
> should be:
> >>> $autoid = mysql_insert_id($db); // you send the resource of your MySQL
> >>> connection (http://ca3.php.net/manual/en/function.mysql-insert-id.php)
> >>> This should be corrected in everywhere you call mysql_insert_id()
> >>>
> >>> -Lex
> >>>
> >>>
> >>>
> >> I tried this (from the link above)
> >> $result1 = mysql_query($sql1, $db);
> >> Â  Â $autoid = mysql_insert_id();
> >> Â  Â echo $autoid;
> >> works...
> >>
> >> but now, I have another problem... I am trying to debug this thing by
> >> going 1 query at a time ( I comment out the rest): every time I do an
> >> INSERT INTO book... the insert works fine but the ID is increased not
> >> from the highest id value but from the last id inserted. So my highest
> >> at the moment is 11; I insert test data and it goes in as id = 15; I
> >> delete this field and redo an insert and it goes in as id = 16. How can
> >> I change this so the auto_increment continues from the 11 ?
> >>
> >
> > In MySQL,
> >
> > ALTER TABLE [tablename] AUTO_INCREMENT = [value];
> >
> > HTH,
> >
> Ok, but why is it doing that? I like to "understand" - I must have asked
> too many questions when I was a kid... it stuck. :-)
>

It's doing that because an auto-incrementing column keeps incrementing with
each insertion. Thus, say you insert an entry into ID #10 and delete it
before another entry is made.  MySQL will still have the auto_increment set
to 11 (one more than the last insertion) rather than subtracting one because
you deleted #10.  It's the database method of ensuring you aren't
over-writing pre-existing auto_increment values.

NOTE: This doesn't take into account someone entering their own value into
the ID field greater than the current auto_increment value.


Re: [PHP] validation & inserts not working

2009-03-11 Thread Lex Braun
PJ,

> 
> > $sql1 = "INSERT INTO book ( title, sub_title, descr,
> >comment, bk_cover, copyright, ISBN, language, sellers )
> > VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN',
> >'$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN',
> >'$sellersIN')";
> >$result1 = mysql_query($sql1, $db);
> >$autoid = mysql_insert_id($result1);
>

You're actually sending mysql_insert_id() the wrong parameter. It should be:
$autoid = mysql_insert_id($db); // you send the resource of your MySQL
connection (http://ca3.php.net/manual/en/function.mysql-insert-id.php)
This should be corrected in everywhere you call mysql_insert_id()

-Lex


Re: [PHP] validation & inserts not working

2009-03-10 Thread Lex Braun
PJ,

On Tue, Mar 10, 2009 at 3:46 PM, PJ  wrote:

> 
> $sql1 = "INSERT INTO book ( title, sub_title, descr,
>comment, bk_cover, copyright, ISBN, language, sellers )
> VALUES ('$titleIN', '$sub_titleIN', '$descrIN', '$commentIN',
>'$bk_coverIN', '$copyrightIN', '$ISBNIN', '$languageIN',
>'$sellersIN')";
>$result1 = mysql_query($sql1, $db);
>$autoid = mysql_insert_id($result1);
>}/* <--- IF THIS LIKE IS DELETED, THE PAGE DOES NOT DISPLAY
> So, if I select insert on the page, only the first query is executed
> since the rest is commented out. Thus, I get onle the book table
> inserted but not other tables, like author, book_author, or publishers,
> book_publisher or categories, book_categories...
> Is there something wrong with what follows immediately...
> like, do I have the brackets right? I've tried about every
> combination possible with no change.
>
> //Check if Author is entered & exists
> if( (strlen($_POST["first_nameIN"]) > 0) && (strlen($_POST["last_nameIN"])
> > 0) ) {
>  $sql2 = "SELECT (first_name, last_name)
>  FROM author WHERE (first_name LIKE '$first_nameIN'
>  && last_name LIKE '$last_nameIN)'";


LIKE is going to do full-text search, which isn't what you want when
searching for a specific author. You can have the query return the ID for
that specific author (to use in your $sql2a query).
$sql2 = "SELECT id FROM author WHERE first_name = '$first_nameIN' AND
last_name = '$last_nameIN' ";

$result2 = mysql_query($sql2);
> if (mysql_num_rows($result2) > 0) {

   $row = mysql_fetch_assoc($result2); // gives you the row return
from $sql2

>
>$sql2a = "INSERT INTO book_author (authID, bookID, ordinal)
>VALUES (author.id WHERE (first_name LIKE '$first_nameIN'
> && last_name LIKE '$last_nameIN'),
> book.ID WHERE book.title LIKE '$titleIN'), '1'";

With the change in $sql2 and the fact that the bookID is stored in $autoid,
this becomes:
$sql2a = "INSERT INTO book_author (authID, bookID, ordinal) VALUES (" .
$row['id'] . ", " . $autoid . ", '1')";

>
> $result2a = mysql_query($sql2a, $db);
>}
>  elseif (mysql_num_rows($result2) = 0) {
>  $sql2b = "INSERT INTO author (first_name, last_name)
>VALUES ('$first_nameIN', '$last_nameIN')";
> $result2b = mysql_query($sql2b, $db);

$authorID = mysql_insert_id($result2b); // gives you the id
of the newly inserted author for use in your book_author table

>
>  $sql2c = "INSERT INTO book_author (authID, bookID, ordinal)
>VALUES (author.id WHERE (first_name LIKE '$first_nameIN'
>&& last_name LIKE '$last_nameIN'), book.ID
>WHERE book.title LIKE '$titleIN'), '1'";

With the addition of $authorID and the fact that bookID is stored in
$autoid, your $sql2c should now be:
$sql2c = "INSERT INTO book_author (authID, bookID, ordinal) VALUES (" .
$authorID . ", " . $autoid . ", '1')";

>
> $result2c = mysql_query($sql2c, $db);
>}
>}


- Lex


[PHP] Re: [PHP-DB] Re: [PHP] Re: [PHP-DB] Re: Problems with displaying results

2009-03-05 Thread Lex Braun
On Thu, Mar 5, 2009 at 10:30 AM, Terion Miller wrote:

> Still having problems with getting this script to work, the first part of
> the query does now work since I used the suggested JOIN, so my results are
> there and I can echo them but now I can't seem to get them to display
> neatly
> somehow:
>
> CODE THUS
> FAR
>   $query =  "SELECT admin.UserName, admin.AdminID, workorders.WorkOrderID,
> workorders.CreatedDate, workorders.Location, workorders.WorkOrderName,
> workorders.FormName, workorders.STATUS, workorders.Notes, workorders.pod
> FROM admin LEFT JOIN workorders ON (admin.AdminID = workorders.AdminID)
> WHERE admin.Retail1 = 'yes'
> ";
>
>$result = mysql_query ($query) ;
>//$row = mysql_fetch_array($result);
>while ($row = mysql_fetch_row($result)){
> $admin_id = $row['AdminID'];


mysql_fetch_row() returns a numerical array (
http://ca2.php.net/manual/en/function.mysql-fetch-row.php), but then you are
trying to assign $admin_id using an associative array. Thus, you need to
either return your row as an associative array (
http://ca2.php.net/manual/en/function.mysql-fetch-assoc.php) or assign
$admin_id as a numerical array:

Method 1: Use a numerical array
$result = mysql_query($query);
while($row = mysql_fetch_row($result)) {
$admin_id = $row[1]; // since it's the 2nd item in your SELECT
...
}

OR

Method 2: Use an associative array
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { // returns result row as an
associative array
$admin_id = $row['AdminID'];

}