Jump to content
Larry Ullman's Book Forums

bahaa

Members
  • Posts

    147
  • Joined

  • Last visited

Posts posted by bahaa

  1. My sql query would be like this

     

    $sql ="SELECT CategoryID, category.Title, Visible, Ordering, category_position.Title, CreationDate,

    CreatedBy, LastEdited, LastEditedBy

    FROM category

    LEFT JOIN category_position USING(PositionID)

    LEFT JOIN country_category USING(CategoryID)";

    if($Visible ==1 OR $Visible ==1)

    {

    $sql .=" WHERE Visible = ? ";

    }

    $sql .=" ORDER BY Ordering ";

     

    How do I do the binding if they user did not choose to sort the outpout ?

  2. Thanks Larry

     

    I have another question.

    I have a page that display data from the database and I want to make sorting options by visibility and country.

     

    How do I bind an optional parameter to the prepared statement ?

     

     

    I know how to constrcut the the sql query but my problem is with the binding.

     

    Here is my statement

     

     

    function GetCategories()

    {

    global $mysqli;

     

    $sql ="SELECT CategoryID, category.Title, Visible, Ordering, category_position.Title, CreationDate,

    CreatedBy, LastEdited, LastEditedBy

    FROM category

    LEFT JOIN category_position USING(PositionID)

    LEFT JOIN country_category USING(CategoryID)

    ORDER BY Ordering ";

     

     

    //prepare the stmt

    $stmt = $mysqli->prepare($sql);

     

     

    //execute the stmt

    $stmt->execute();

     

    //Store the result

    $stmt->store_result();

     

    //Bind the result to variables

    $stmt->bind_result($CategoryID, $Title, $Visible, $Ordering, $Position, $CreationDate,

    $CreatedBy, $LastEdited, $LastEditedBy);

     

    //Get numRows

    if($stmt->num_rows >0 )

    {

    //Fetch the array

    while($stmt->fetch())

    {?>

     

    <tr>

    <td><input type="checkbox" name="chkCategoryID[]" class="chk" value="<?php echo $CategoryID; ?>"/></td>

    <td><?php echo $Title ;?></td>

    <td><?php echo $Position ; ?></td>

    <td>

    <?php

    if($Visible ==1)

    {

    echo '<img src="../images/icon-16-published.gif" width="16" height="16" alt="منشور" />';

    }

    else

    {

    echo '<img src="../images/icon-16-unPublished.gif" width="16" height="16" alt="منشور" />';

    }

    ?>

    </td>

    <td><input type="text" name="txtOrdering" value="<?php echo $Ordering; ?>"/></td>

    <td><?php echo $CreationDate; ?></td>

    <td><?php echo $LastEdited; ?></td>

    <td><?php echo GetUserByID($CreatedBy); ?></td>

    <td><?php echo GetUserByID($LastEditedBy); ?></td>

    </tr>

    <?php }

    }

     

    //Close connection

    $stmt->close();

     

    //return the result

    return $stmt;

     

    }

  3. Masterlayouts,

     

    I am only inclined to relate the term transactions with something having to do with ecommerce. Does the transaction involve using the session id? It seems that when recording a post data (or many) per unique user, utilizing the session variable is required. Is it not?

     

    The transactions are used if you want to inster multi record to the database and you want to make sure all queries succeed or roll back if something went wrong.

     

    example of using transation is when transfering money from one account to another, if you send money to another acount and the money withdrown from you account and then something happens before the are deposited in the other account then everything roll back.

    So in the bank account example when useing the transation is forcing to the queries to roll back if something goes wrong.

  4. if the code you wrote it's correct than the function will return your $select, so I guess what you want is to display it in which case it should be as simple as
    echo $select;

     

    My function works, but what I want the fetching to happen out of the function not inside.

    In the one above the fetching is happening inside the function and I call it on my page to dispay the drop down.

  5. Thanks for all your responses.

     

    To both Jonathan and Antonio, thanks for presenting the script in different ways. It helped me understand what was really going on behind the scenes. I could not understand it at first, I was also unsure about the way I comprehended it.

     

    But to reiterate (directed at Antionio's explanation):

     

     

    In it's most simplest form:

     

    The variable takes on a new value each time it is passed through the ternary operator, correct?

     

    Either way, I believe this is how I think it works. It makes sense.

     

    @bahaa - Your suggestion also a worth considering in future projects. This method will be most widely used once CSS3 is most widely supported by all the browsers.

    It is already supported by major browser and also on IE 9

    • Upvote 1
  6. Here is 2 functions to check for unique email address, one used for insert and the other for update

    when you try to update a record and want to check for unique email, you have to check for all records except the one you are trying to use

     

    function uniqueEamilUpdate($email, $ID){

    global $sdbc;

    $query = "SELECT userEmail, userID

    FROM user

    WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."'

    AND userID !='". mysqli_real_escape_string($sdbc, $ID)."'";

    $result = mysqli_query($sdbc, $query);

    return $result;

    }

     

     

     

    and here is a function to check for unique email on insert

     

    function uniqueEamil($email){

    global $sdbc;

    $query = "SELECT userEmail, userID

    FROM user

    WHERE userEmail ='". mysqli_real_escape_string($sdbc, $email)."'";

    $result = mysqli_query($sdbc, $query);

    return $result;

    }

  7. You don't need to use it anymore for alternat background color and you can use css instead

     

     

    you can use this selector tr:nth-child(odd)

     

     

    here is an example where I use it

     

    #container #content-box .content-elements .tblListData tbody tr:nth-child(odd){

    background: #fff;

    border-top: 10px solid #FFF;

    }

    • Upvote 1
  8. Hello,

     

     

    I recently started using the mysqli prepared statement and I am not so good with them yet.

     

    I have a mysqli prepared statement inside a function and it works fine but my problem is that I don't know how to fetch a mysqli prepared statement out side the function.

     

    here is my function

     

     

    function GetCountriesList()

    {

    global $mysqli;

     

    //Build sql query

    $sql ="SELECT CountryID, Name FROM country";

     

    //Prepare the stmt

    $stmt = $mysqli->prepare($sql);

     

    //execute the stmt

    $stmt->execute();

     

    //store the result

    $stmt->store_result();

     

    //bind the result

    $stmt->bind_result($CountryID, $Country);

    $select= "<select name=\"cboCountry\">";

    $select .="<option value=\"0\">الدولة</option>";

    //fetch the result

    while($stmt->fetch() )

    {

    $select .= "<option value=\"{$CountryID}\">{$Country}</option>";

    }

    $select .="</select>";

    return $select;

    }

     

     

    How do I fetch the result out side the function ?

     

     

    PHP/5.3.4

    MySQL client version: mysqlnd 5.0.7-

  9. What is the problem with the constraints?

    Using the sql statement you provided, how would I get the online registered users and online guests? what should I do if I only want to get the online loggedin user in backened?

    What If I want to get the name of the onlined logged in users?

     

    If I want to know more about the strucuter of the whole database I would send it to you in private message.

  10. I am using MySQL database and here is the session table

     

    CREATE TABLE IF NOT EXISTS `session`(

     

    sessionID CHAR(32) NOT NULL,

    sessionData MEDIUMTEXT NOT NULL,

    sessionTime TIMESTAMP NOT NULL,

    sessionGuest TINYINT UNSIGNED NOT NULL ,

    userID INT UNSIGNED COMMENT 'Fk to the user table',

     

    CONSTRAINT pk_session_sessionID PRIMARY KEY(sessionID),

    CONSTRAINT fk_user_session_userID FOREIGN KEY(userID) REFERENCES user(userID),

    INDEX(sessionTime),

    INDEX(sessionGuest)

     

     

    )ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

     

  11. Hi all,

     

    I have a table with these fields :articalID, articalTitle, articalBody, articalPhoto, articalTags, articalPublishDate, articalView, articalVisible, userID, catID

     

    The articalPublishDate is a datetime type. FROM the CMS I set the date and times when the artical to be released or published. How do compare the articalPublishDate with the date and time now and select the row if date and time now equal or greater than the date and time in the table?

  12. So basically a query within a query? I suppose that would work, but I can't understand why you wouldn't just auto increment the ID field instead.

    If I was working on this project, I would use the auto increment, but we are taking this in school and the teacher meant to not auto increment the ID field for teaching purposes , so she can show us how to use the sub query.

×
×
  • Create New...