1

(11 replies, posted in PHP)

Hi,

I am not sure about your foreach "(get_xml_face() as $product) " ,so I just try this simple example with include or require, I don't know if this can help you a bit!
File test.php:

<?php
function render_data($arr) {
    $output = '<table width="100%" border="1"><tr>';
    require("include.php");
    include("include.php");
    $output .= '</tr></table>';
    return $output;
}
 
$arr = array("Homepage","About Us","Portfolio","Help");
print render_data($arr);
?>

Here is the file that I use to include or require include.php:

<?php 
for($i= 0; $i<=3; $i++){
    $output .= '<td>'.$arr[$i];
    $output .= '</td>';
}
?>

Quyen,

2

(7 replies, posted in PHP)

You are welcome!

3

(11 replies, posted in PHP)

Hello,

you can use "require", "include" to  include files. Its pretty hard to look at your code right now, next time could you please put it in

Your php code will show here

and also share us a full php file.

Did you mean that is there anyone can help you to find the common code from your example and put it in a new file? so later you can reuse this file? Or did you mean something else?

Quyen,

4

(7 replies, posted in PHP)

Hi again,

Could you please send me the php file, and I will try to edit it for you. Or you can try this:

$bno = mysql_real_escape_string(htmlspecialchars($_POST['branch_id']));
$street = mysql_real_escape_string($_POST['street']);
$area = mysql_real_escape_string(htmlspecialchars($_POST['area']));
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));
$postcode = mysql_real_escape_string(htmlspecialchars($_POST['postcode']));
$telno = mysql_real_escape_string(htmlspecialchars($_POST['tel']));
$faxno = mysql_real_escape_string(htmlspecialchars($_POST['fax']));

Note:
branch_id, street,  area, city, postcode, tel, fax have to be exacly the same with form_component_name (I am not sure what do they look like now after you edit).
In the code above, for example your input text is :
        <input type='text' name='branch_id' value='anything' />
        <input type='text' name='street' value='anything' />
        <input type='text' name='area' value='anything' />
        ....do the same to other components.
Or you can try to read this page, very basic and simple, also easy to understand : http://w3schools.com/php/php_post.asp

Goodluck!

Quyen,

5

(7 replies, posted in PHP)

Hello Taksi10,

I think the problem comes from the form. Check your function renderForm() again, and also give the input fields different names.

I hope that will help!
Quyen,

6

(7 replies, posted in PHP)

24hourapartments wrote:

<DIV align="ceneter"> not working.

Can anyone tell what's the problem?

--Scott

Hello,

Try this one:
<div align="center">Your text will show here</div>

Quyen,

Hello everyone,

Thanks a lot for your all replies, I do want to post all the coding which are require or include for you, but I'm wondering that my php pages are quite long, I hope you guys can understand all.
Please let me know if there is anything wrong. Sorry  I didn't comment much in my php code, I should do that but sorry i am too busy. If you need any futher information, let me know pls.
Thanks a lot again

"The main problem occurs in menu_manager.php file, I think"
Quyen,

file content.php:

<div class="page-wrapper">
<?php 
if(!isset($_SESSION['user_id'])){
    header("location: alert_box.php?opt=no_permission");
}
else{
    if(isset($_GET["content"])){
        $content = $_GET["content"];
        switch($content){
            case "home":
            break;
            case "menus":
                require_once("menu_manager.php");
            break;
            case "users":
                require_once("user_manager.php");
            break;
            case "portfolio":
            break;
            case "cart":
            break;
        }
    }
}
?>
</div>

file menu_manager.php:

<?php                
    $query = "SELECT * FROM menu 
              ORDER BY last_edit DESC";
    $result = mysql_query($query);
    $cnt_records = mysql_num_rows($result);
    if($cnt_records){
    $per_page = 10;
    $start = 0;
    $num_of_page = ceil($cnt_records/$per_page);
    if(isset($_GET['page'])){
        $get_request = $_GET['page'];
        if($get_request!="")
        {
            $start = ($get_request-1) * $per_page;
        }
        else
        {
            $start = 0;
        }
    }
    else{
    }
    ?>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    
        <div class="sub-menuitems">
            <form name="formPaging" id="formPaging" action="index.php?content=menus" method="post"> 
            <?php 
            if($num_of_page >=2){
            ?>
            <div class="items">
                Page:
                <select name="page" id="page" onchange="document.formPaging.submit()">
                    <option selected="selected">Select one</option>
                    <?php 
                    for($index=1;$index <= $num_of_page;$index++)
                    {
                        if($get_request == $index)
                        {
                            
                            echo "<option value=$index>$index </font>";
                        }
                        else
                        {
                            echo "<option value=$index>$index </font>";
                        }    
                    }
                    ?>
                </select>
                <?php 
                if(isset($_POST['page'])){
                    $selected = $_POST['page'];
                    for($index=1;$index <= $num_of_page;$index++)
                    {
                        if($get_request == $selected)
                        {
                            header("location: index.php?content=menus&page=$selected");
                        }
                        else
                        {
                            header("location: index.php?content=menus&page=$selected");
                        }    
                    }
                }
                ?>
            </div>
            <?php 
            }
            ?>
            </form>
            <form name="formMenuitemQuickLaunch" id="formMenuitemQuickLaunch" action="index.php?content=menus&act=item" method="post">
            <div class="items">   
                Item: 
                <select name="menu_id" id="menu_id" onchange="document.formMenuitemQuickLaunch.submit();">
                    <option>Select one</option>
                <?php 
                if($cnt_records){
                    while($row = mysql_fetch_array($result)){
                        ?>
                        <option value="<?php print $row['menu_id'];?>">
                            <?php 
                            print $row['name']."--"; 
                            if($row['type']==0){print "B";}else{print "F";}
                            ?>
                        </option>
                        <?php
                    }
                }
                ?>
                </select>
            </div>
            </form>
            <form name="formMenuGroup" id="formMenuGroup" action="index.php?content=menus&page=1" method="post"> 
            <div class="items">
                Quick launch: 
                <select name="menuType" id="menuType" onchange="document.formMenuGroup.submit();">
                    <option value="none">Select one</option>
                    <option value="all">All</option>
                    <option value="backend">Back-End</option>
                    <option value="frontend">Front-End</option>
                    <option value="published">Published</option>
                    <option value="unpublished">Un-Published</option>
                </select>
            </div>   
            </form> 
        </div>
        <div class="sub-menuitems">
            <div class="items">
                <a href="#">Delete</a>
            </div>
            <div class="items">
                <a href="#">Published</a>
            </div>
            <div class="items">
                <a href="#">None</a>
            </div>
            <div class="items">
                <a href="#" onclick="">Select All</a>
            </div>
            
            <div class="items">
                <a href="index.php?content=menus&act=formAdd">Add</a>
            </div>
        </div>
        <?php 
        if(!isset($_GET['act'])){
            require_once("menu_content.php");
        }
        else{
            $action = $_GET['act'];
            switch($action){
                case"item":
                    if(isset($_POST['menu_id'])){
                        $menu_id = $_POST['menu_id'];
                        $query = "SELECT * FROM menu
                                  WHERE menu_id = '$menu_id'
                                  ";
                        $result_items = mysql_query($query);
                        $cnt_records = mysql_num_rows($result_items);
                        if($cnt_records){
                        ?>
                        <div class="sub-content">
                        <table>
                            <tr class="col-title">
                                <td>ID</td>
                                <td>Group</td>
                                <td>Name</td>
                                <td>Link name </td>
                                <td>Ordering</td>
                                <td>Published</td>
                                <td>Details</td>
                                <td>Edit</td>
                                <td>Delete</td>
                                <td>#</td>
                            </tr>
                        <?php
                            while($row = mysql_fetch_array($result_items)){
                                require("menu_list.php");
                            }
                        ?>
                        </table>
                        </div>
                        <?php    
                        }
                    }
                break;
                
                case "pos":
                    if(isset($_POST['pos'])){
                        $ordering= $_POST['pos'];
                        $menu_id = $_POST['menu_id'];
                        $last_edit = date("F j, Y, g:i a");
                        $query = "UPDATE menu
                                SET ordering = '$ordering',last_edit = '$last_edit'
                                WHERE menu_id = '$menu_id' 
                                ";
                        $result = mysql_query($query);
                        $cnt_records = mysql_num_rows($result);
                        if($result){
                            header("location: index.php?content=menus");
                        }
                    }
                break;
                case 'formAdd':
                    require_once("menu_adding_form.php");
                break;
                case 'add':
                    $type = $_POST['type'];
                    $name = $_POST['name'];
                    $link_to = $_POST['link_to'];
                    $ordering = $_POST['ordering'];
                    $created_date = date("F j, Y, g:i a");
                    $last_edit = date("F j, Y, g:i a");
                    if(isset($_POST['published'])){
                        $status = 1;
                    }
                    else{
                        $status = 0;
                    }
                    if($name == ""){
                        header("location: alert_box.php?opt=empty");
                    }
                    else{
                        
                        $table = "menu";
                        $cols = "menu_id,type,name,link_to,ordering,
                                 status,created_date,last_edit
                                 ";
                        $values = "'','$type','$name','$link_to','$ordering',
                                  '$status','$created_date','$last_edit'
                                  ";
                        $query = "INSERT INTO menu ($cols) 
                                  VALUES ($values)
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("location: index.php?content=menus");
                        }
                        else{
                            header("location: alert_box.php?opt=not_insert");
                        }
                    }
                break;
                case "more":
                    if(isset($_GET['id'])){
                        $menu_id = $_GET['id'];
                        $query = "SELECT * FROM menu 
                                  WHERE menu_id = '$menu_id'
                                  ";
                        $result = mysql_query($query);
                        $cnt_records = mysql_num_rows($result);
                        if($cnt_records > 0){
                            require("menu_editing_form.php");
                        }
                    }
                break;
                case "edit":
                    $menu_id = $_POST['menu_id'];
                    $type = $_POST['type'];
                    $name = $_POST['name'];
                    $link_to = $_POST['link_to'];
                    $ordering = $_POST['ordering'];
                    $last_edit = date("F j, Y, g:i a");
                    if(isset($_POST['published'])){
                        $status = 1;
                    }
                    else{
                        $status = 0;
                    }
                    if($name == ""){
                        header("location: alert_box.php?opt=empty");
                    }
                    else{
                        $set = "type = '$type', 
                                name = '$name',
                                link_to = '$link_to',
                                ordering = '$ordering', 
                                status = '$status', 
                                last_edit = '$last_edit' ";
                        $query = "
                                 UPDATE menu
                                 SET $set 
                                 WHERE menu_id = '$menu_id'
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("location: index.php?content=menus");
                        }
                        else{
                            header("location: alert_box.php?opt=not_updated");
                        }
                    }
                break;
                case "delete":
                    if(!isset($_GET['id'])){}
                    else{
                        $menu_id = $_GET['id'];
                        $query = "DELETE FROM menu
                                  WHERE menu_id = '$menu_id';
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("index.php?content=menus");
                        }
                        else{
                            print mysql_error();
                            
                            header("alert_box.php?opt=delete_error");
                        }
                    }
                break;
                
                case "status":
                    if(isset($_GET['id']) && isset($_GET['cur'])){
                        $menu_id = $_GET['id'];
                        $last_edit = date("F j, Y, g:i a");
                        $current_status = $_GET['cur'];
                        if($current_status == 0){
                            $status = 1;
                        }
                        elseif($current_status == 1){
                            $status = 0;
                        }
                        else{
                            header("location: alert_box.php?opt=no_permission");
                        }
                        $query = "UPDATE menu
                                  SET status = '$status', last_edit = '$last_edit' 
                                  WHERE menu_id = $menu_id
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("location: index.php?content=menus&page=1");
                        }
                        else{
                            header("location: alert_box.php?opt=not_updated");
                        }
                    }
                break;
            }
        }
    } 
?>

Required files:
menu_list.php

<tr>
    <td>
        <?php print $row['menu_id']?>
    </td>
    <td>
        <?php 
        if($row['type'] == 1){
            print "Front-end";
        }
        else{
            print "Back-end";
        }
        ?>
    </td>
    <td>
        <?php print $row['name'];?>
    </td>
    <td>
        <?php print $row['link_to'];?>
    </td>
    <form name="orderingForm" id="orderingForm" action="index.php?content=menus&act=pos&page=1" method="post">
    <td>
        <?php 
            print $row['ordering'];
            $current_id = $row['menu_id'];
        ?>
        <input type="hidden" name="menu_id" id="menu_id" value="<?php print $current_id;?>" />
        <?php 
        if(!isset($_GET['act'])){
        }
        else{
            ?>
            <select name="pos" id="pos" onchange="submitFormOrdering()">
                <option selected="selected">Select</option>
                <?php 
                $query = "SELECT * FROM menu";
                $result = mysql_query($query);
                $cnt_records = mysql_num_rows($result);
                for($i= 1; $i<= $cnt_records; $i++){
                    ?>
                    <option><?php print $i;?></option>
                    <?php
                }
                ?>
            </select>
         <?php 
             }
         ?>
    </td>
    </form>
    <td>
        <?php 
        if($row['status'] == 1){
            print "<a href='index.php?content=menus&act=status&id="; print $row['menu_id']."&cur="; print $row['status']."'><font color='#FF3300'>Yes</font> </a>";
        }
        else{
            print "<a href='index.php?content=menus&act=status&id="; print $row['menu_id']."&cur="; print $row['status']."'><font color='#006600'> No </font> </a>";

        }
        ?>
    </td>
        <td>
            <a href="index.php?content=menus&act=more&id=<?php print $row['menu_id'];?>">Detail</a>
        </td>
    <td>
        <a href="index.php?content=menus&act=more&id=<?php print $row['menu_id'];?>">Edit</a>
    </td>
    <td>
        <a href="index.php?content=menus&act=delete&id=<?php print $row['menu_id'];?>">Delete</a>
    </td>
    <td>
    <form name="multiSelectedForm" id="multiSelectedForm">
        <input type="checkbox" name="selectedItems" id="selectedItems" />
    </form>
    </td>
</tr>

menu_adding_form.php

<link href="css/style.css" rel="stylesheet" type="text/css" />
<div class="sub-content">
    <table>
        <tr class="form-title">
            <td colspan="2">Add a new menuitem</td>
        </tr>
        <form name="menuAddingForm" id="menuAddingForm" method="post" action="index.php?content=menus&act=add">
        <tr>
            <td class="comp-title">Group of menu </td>
            <td>
                <select name="type" id="type">
                    <option value="1">Front-End</option>
                    <option value="0">Back-End</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="comp-title">Menuitem name</td>
            <td><input type="text" name="name" id="name" /></td>
        </tr>
        <tr>
            <td class="comp-title">Link to</td>
            <td>
                <select name="link_to" id="link_to">
                    <option value="home">Home page</option>
                    <option value="aboutus">About Us</option>
                    <option value="webdesigns">Web Designs</option>
                    <option value="webtemplates">Web Templates</option>
                    <option value="contact">Contact Us</option>
                    <option value="cart">Shopping Cart</option>
                    
                    <option value="menus">Menus</option>
                    <option value="users">Users</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="comp-title">Ordering</td>
            <td>
                <select name="ordering" id="ordering">
                    <?php 
                    $query = "SELECT * FROM menu";
                    $result = mysql_query($query);
                    $cnt_records = mysql_num_rows($result);
                    for($i= 1; $i<= $cnt_records; $i++){
                        print '<option>'.$i.'</option>';
                    }
                    ?>
                    <option><?php print $cnt_records + 1?></option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="comp-title">Published</td>
            <td>
                <input type="checkbox" name="published" id="published"/>
            </td>
        </tr>
        <tr>
            <td class="comp-title"> Enter or click
            </td>
            <td>
                <input type="submit" value="Save this record"/>
                <input type="reset"  name="reset" value="Reset"/>
                <input type="reset"  name="cancel" value="Cancel" onClick="javascript: history.go(-1);"/>
            </td>
        </tr>
        </form>
    </table>
</div>

menu_editing_form.php

<div class="sub-content">
    <table>
        <tr class="form-title">
            <td colspan="2">Detail of menuitems <font color="#CC3300"> " <?php print $menu_id; ?> "</font></td>
        </tr>
        <form name="menuEditingForm" id="menuEditingForm" method="post" action="index.php?content=menus&act=edit">
        <tr>
            <td class="comp-title">Group of menu </td>
            <td>
                <select name="type" id="type">
                    <?php 
                    while($row = mysql_fetch_array($result)){
                    ?>
                    <option value="<?php  print $row['type'];?>" selected>
                        <?php 
                        $menu_id = $row['menu_id'];
                        $type = $row['type'];
                        $name = $row['name'];
                        $link_to = $row['link_to'];
                        $status = $row['status'];
                        $ordering = $row['ordering'];
                        $created_date = $row['created_date'];
                        $last_edit = $row['last_edit'];
                        if($type ==  0){
                            print "Back-End";
                        }
                        else{
                            print "Front-End";
                        }
                        ?>
                    </option>
                    <?php 
                    }
                    if($type == 0){
                        ?>
                        <option value="1" >Front-End</option>
                        <?php
                    }
                    else{
                        ?>
                        <option value="0" >Back-End</option>
                        <?php
                    }
                    ?>
                </select>
                <input type="hidden" name="menu_id" id="menu_id" value="<?php print $menu_id; ?>">
            </td>
        </tr>
        <tr>
            <td class="comp-title">Menuitem name</td>
            <td><input type="text" name="name" id="name"  value="<?php print  $name; ?>"/></td>
        </tr>
        <tr>
            <td class="comp-title">Link to</td>
            <td>
                
                <select name="link_to" id="link_to">
                    <option value="<?php print $link_to;?>" selected><?php print $link_to;?></option>
                    <?php 
                    $query = "SELECT * FROM menu
                              WHERE link_to <> '$link_to'
                              ORDER BY ordering ASC
                             ";
                    $result = mysql_query($query);
                    $cnt_records = mysql_num_rows($result);
                    if($cnt_records){
                        while($row = mysql_fetch_array($result)){
                            $type = $row['type'];
                        ?>
                        <option value="<?php print $row['link_to'];?>">
                            <?php 
                            print $row['link_to']."--";
                            if($type == 0){
                                print "Back";
                            }
                            else{
                                print "Front";
                            }
                            ?>
                        </option>
                        <?php
                        }
                    }
                    ?>
                </select>
            </td>
           
        </tr>
        <tr>
            <td class="comp-title">Ordering</td>
            <td>
                <select name="ordering" id="ordering">
                    <?php 
                    $query = "SELECT * FROM menu";
                    $result = mysql_query($query);
                    $cnt_records = mysql_num_rows($result);
                    for($i= 1; $i<= $cnt_records; $i++){
                    $start = $i + 1;
                    ?>
                    <option <?php if($ordering == $i){?>selected="selected" <?php }?>>
                        <?php  print $i;?>
                    </option>
                    <?php
                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td class="comp-title">Published</td>
            <td>
                <input type="checkbox" name="published" id="published" <?php if ($status == true){ ?>checked<?php } ?> />
            </td>
        </tr>
        <tr>
            <td class="comp-title">Created on </td>
            <td>
                <label><?php print $created_date; ?></label>
            </td>
        </tr>
        <tr>
            <td class="comp-title">Last edited</td>
            <td>
                <label><?php print $last_edit ; ?></label>
            </td>
        </tr>
        <tr>
            <td class="comp-title"> Enter or click
            </td>
            <td>
                <input type="submit" value="Save this record"/>
                <input type="reset"  name="reset" value="Reset"/>
                <input type="reset"  name="cancel" value="Cancel" onClick="javascript: history.go(-1);"/>
            </td>
        </tr>
        </form>
    </table>
</div>

8

(4 replies, posted in PHP)

are you selecting form the same table or from two different tables? You can build your select query better with sub-query. and make your code more simple and easy to handle.

I think you are selecting from 1 table (or even 2 tables), the thing is you want to delete the records where the conditions are true, you can also use:

if(condition1 && condition2){
 
}
else{

}

Quyen,

Yeah this is my php code: it may show you something easy to understand.

                case 'formAdd':
                    require_once("menu_adding_form.php");
                break;
                case 'add':
                    $type = $_POST['type'];
                    $name = $_POST['name'];
                    $link_to = $_POST['link_to'];
                    $ordering = $_POST['ordering'];
                    $created_date = date("F j, Y, g:i a");
                    $last_edit = date("F j, Y, g:i a");
                    if(isset($_POST['published'])){
                        $status = 1;
                    }
                    else{
                        $status = 0;
                    }
                    if($name == ""){
                        header("location: alert_box.php?opt=empty");
                    }
                    else{
                        
                        $table = "menu";
                        $cols = "menu_id,type,name,link_to,ordering,
                                 status,created_date,last_edit
                                 ";
                        $values = "'','$type','$name','$link_to','$ordering',
                                  '$status','$created_date','$last_edit'
                                  ";
                        $query = "INSERT INTO menu ($cols) 
                                  VALUES ($values)
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("location: index.php?content=menus");
                        }
                        else{
                            header("location: alert_box.php?opt=not_insert");
                        }
                    }
                break;
                case "more":
                    if(isset($_GET['id'])){
                        $menu_id = $_GET['id'];
                        $query = "SELECT * FROM menu 
                                  WHERE menu_id = '$menu_id'
                                  ";
                        $result = mysql_query($query);
                        $cnt_records = mysql_num_rows($result);
                        if($cnt_records > 0){
                            require("menu_editing_form.php");
                        }
                    }
                break;
                case "edit":
                    $menu_id = $_POST['menu_id'];
                    $type = $_POST['type'];
                    $name = $_POST['name'];
                    $link_to = $_POST['link_to'];
                    $ordering = $_POST['ordering'];
                    $last_edit = date("F j, Y, g:i a");
                    if(isset($_POST['published'])){
                        $status = 1;
                    }
                    else{
                        $status = 0;
                    }
                    if($name == ""){
                        header("location: alert_box.php?opt=empty");
                    }
                    else{
                        $set = "type = '$type', 
                                name = '$name',
                                link_to = '$link_to',
                                ordering = '$ordering', 
                                status = '$status', 
                                last_edit = '$last_edit' ";
                        $query = "
                                 UPDATE menu
                                 SET $set 
                                 WHERE menu_id = '$menu_id'
                                 ";
                        $result = mysql_query($query);
                        if($result){
                            header("location: index.php?content=menus");
                        }
                        else{
                            header("location: alert_box.php?opt=not_updated");
                        }
                    }
                break;

Thanks a lot
Quyen,

Thanks for your reply,

Yeah you're right, I've used Header(); quite a lot in my php pages, ofcourse I used it for different purposes, I've been trying to comment all the //Headers, and use print "somthing"; to replaces Header commands, it works fine, but Whenever I remove one of the comment lines, the warning will appear straight away.

I am sure I did something wrong, but I haven't found it yet. Maybe I already used Header in the file which is required in the php page. But I think you can use multiple Headers command on the same page or include or require, right?

Thanks a lot for your help but really I don't want to use javascript in this case, because  users can turn it off anytime, that means my website won't work ok without JS

Quyen,

11

(4 replies, posted in PHP)

Hello,
I've just read your message, I think you should try this:

<html>
<body>
<?php
$arr = array("record1", "record2", "record3", "conditions");
$cond = "conditions";
foreach ($arr as $key => $value) {
    if($value == $cond){
        print  "<br><br><font color='red'><b>$value</b></font>
                 <input type='button' value='Cannot Delete'><br><br>
              ";
    }
    else{
        print "<br><br><font color='blue'><b>$value</b></font>
                <input type='button' value='Delete'><br><br>
                ";
    }
}
?>
</body>
</html>

I hope this can help you a bit,
Goodluck!
Quyen,

I've been trying to solve my problem by using http://www.desilva.biz/php/phprefresh.html and http://www.tizag.com/javascriptT/javasc … irect.php, but I haven't solved my problems yet, I will try some other solutions, but I am still looking for someone to help me. I know "Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\index.php:15) in C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\user_manager.php on line 319" is just a warning, not an error, but the thing is I don't want this warning to show up on the page, and whenever this warning is occurs, it stops redirecting. I will try to use Adam's solution and see what'll happen....

Thank you very much Adam,
Quyen,

Thanks a lot Wickham and falkencreative, I am sure I can solve my problems soon. Thanks again.

Quyen,

Well, I've tried that before but when you use headers to show a message before redirect to a page, actually you cannot see the message, the headers will execute straight away.
Anyway thanks a lot !
Quyen,

Thanks for you reply,

I might have look at those links, and try to fix my problems. By the way, Do you know somehow we can redirect to a page without using header("Location: $url") ?.

In my case: I want to show a message before redirect to any pages?

Thank you very much for that.
Quyen,

Hello everybody,

This is a warning i've got whenever I use "header("Location: $url"); ".

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\index.php:15) in C:\Inetpub\vhosts\qwebdesign.net\httpdocs\admin\user_manager.php on line 319

Is there any solution to redirect to a url without using "header("Location: $url"); Or somehow I can fix the above problem?

I've been reading some topics on some websites, and they are all about how to use header("Location: $url"); but i haven't found any right answer for my situation.

Note: "header("Location: $url");" runs ok on localhost at my computer,  I only got this problem when I upload my project to a hosting.

Thank a lot for helping me!
Quyen,

Hi,

I just help you to fix the problem. I hope it works fine on your pc. Have a nice day (~_~) !

<?php 
   /* BEFORE YOU RUN THIS FILE, YOU SHOULD CHANGE THE PASSING VALUES  FOR CONSTRUCTOR.
       * THEN IT WILL WORK OK.
    */
   //////////////////////////////////////////////////////////////////////////////////
   
    /*  Acctually This object connect to db, but haven't done anything on the 
     *    database 'mysql' yet.
     */
    $myObj = new DBConnect("localhost","root","","mysql");
    $myObj ->connectToMySQL();
    
    /*
        You need to pass the values to the arguments for the constructor from 
        superclass DBConnect. If not $dbData won't know how to connect to database.
    */
    $dbData = new DBData("localhost","root","","information_schema");
    
    /*
        You need to connect to MySQL before work on any databases.
    */
    $dbData ->connectToMySQL();
    
    /*
        - Because the class DBData defined only one method selectDB(); 
        - Whenever you want to work with a database, you need to connect to MySQL first.
        - In your situation, You didn't call method connectToMySQL() from superclass DBObject.
        --> That's why You cannot select any databases.(Thats why you've got the errors)
    */
    $dbData ->selectDB();

    class  DBConnect{
        /////////////////////////////////////////////////
        // PROTECTED PROPERTIES  
        /////////////////////////////////////////////////
        protected $hostname, $username, $password,$db_name, $con ;
        
     /**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        public function __construct($hostname,$username,$password,$db_name){

            $this->hostname = $hostname;
            $this->username = $username;
            $this->password = $password;
            $this->db_name = $db_name;

        }
        
     /**
       * Connect to MySQL.
       * @return void
       */
        public function connectToMySQL(){
            $this->con = mysql_connect($this->hostname,$this->username,$this->password);
            if($this->con){
                echo "<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( "<br>Could not connect to MySQL" . mysql_error() . ".<br />");
            }
        }
      }      
        
    /*  
     * DBData class extends DBConnect
     */
    class DBData extends DBConnect{
        /* This class doesn't define any constructors. Whenever you create a object from  
         * this class, The object will automaticaly refer to constructor of 
         * super class (DBObject class). cause DBData extends from DBConnect class 
         */ 
        public function selectDB(){
            $result = mysql_select_db($this->db_name,$this->con);
            if($result){
                echo "<br>Connected sucessfully to database ' ".$this->db_name." ' .<br>";
            }
            else{
                die( "<br>Could not connect to database ' ". $this->db_name. " '  ".mysql_error() . ".<br />");
            }
        }
    }
    
    /*     You don't need to create a class just for closing a connection. 
     *    This method you can build  in DBConnect or DBObject class. But anyhow this is your option
     *    Maybe you have some special reason to have this class.
     */   
    class DBClose extends DBConnect{
        function closeConnection(){
           mysql_close();
        }
    }
    
    //GOODLUCK

?>

Quyen,

russellharrower wrote:

I am getting an error, and i am unsure what i have done wrong.
LIVE demo - http://www.designvisa.com/beta/include/ … opendb.php

$vconfig = new VConfig();

    //Create objects from DBObject class.
    $myObj = new DBConnect("localhost","$vconfig->user","$vconfig->dbpass","$vconfig->db");
    
    //Call methods of objects.
    $myObj ->connectToMySQL();
    
    $myObj2 = new DBData();
    $myObj2 ->selectDB();
    $myObj2 ->showMessage();

class  DBConnect{
        /////////////////////////////////////////////////
        // PRIVATE PROPERTIES  
        /////////////////////////////////////////////////
        private $hostname, $username, $password, $db_name, $con;
        
    /**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        public function __construct($hostname,$username,$password,$db_name){
            $this->hostname = $hostname;
            $this->username = $username;
            $this->password = $password;
            $this->db_name = $db_name;
        }
        
    /**
       * Connect to MySQL.
       * @return void
       */
        public function connectToMySQL(){
            $this->con = mysql_connect($this->hostname,$this->username,$this->password);
            if($this->con){
                echo "<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( "<br>Could not connect to MySQL" . mysql_error() . ".<br />");
            }
        }
        
        
  }      
        
        
        
class DBData extends DBConnect{

 public function selectDB(){
            $result = mysql_select_db($this->db_name,$this->con);
            if($result){
                echo "<br>Connected sucessfully to database ' ".$this->db_name." ' .<br>";
            }
            else{
                die( "<br>Could not connect to database ' ". $this->db_name. " '  ".mysql_error() . ".<br />");
            }
        }
}
    
    
        
        
class DBClose extends DBConnect{
    function closeConnection(){
       //Do something else here and at the end you can
       mysql_close();//For example
    }
}

Can anyone help!

100% sure this won't work. I will reply to you soon, cause i am working now. But goodluck.

russellharrower wrote:

Thanks Quyen, Just wondering the following:

Say i want to have one file that handles connecting to the database. - opendb.php
and one that closes the database - closedb.php

is that possible, and how?

Then say i want another a page to handle MYSQL. - fruitdb.php
How would one go about that?

Hi,
Sorry I haven't replied to you yet! Cause I just take a look at CSS forum(*_*).
About your question:
- To open a connection to a database or close this connection is not complicate, right? But the thing is how to organize your coding in php file?
For me I won't put them in a seperate files. Cause close or open is methods (behaviors) of objects. With mysql_connect() and mysql_close() Why don't you write some methods like this:

function connectToMySQL(){
        //You write your code here
}

function closeConnection(){
       //Do something else here and at the end you can
       mysql_close();//For example
}

And then:
Whenever you create a object from the class above, after you are done everything like select a database, execute sql queries,....After you're done you just need a line of coding like:
objectName.closeConnection();

Notes: For me, I don't care much about number of php files, I care more about what my object can do? Is it enough or need some more fields, methods,.... If your object is perfect, that mean you did a great job (*_*). Don't worry about how many php file you going to create, lets care about how many object do u need in your project? And all fields & methods of your objects are enough or not?

And one more thing I guess you need to read some more is about OOP. Cause whenever you want your object do something, you will go and create a object and then call the suitable method of the object for it.
All the methods you create in your projects are called by objects (*_*).

I am not sure if this could help u! but still hope that you understand what i mean!
Let me know if you need something!
Quyen,

20

(2 replies, posted in CSS)

Hi kitster79,
I just take a look at your html and css file. And now I guess I know why you have the purple background between "#indexThmbs" and "#indexHeader1".

Let edit your css file at this ID = "indexThmbsInner" like this:

#indexThmbsInner {
    width: 900px;
    margin-right: auto;
    margin-left: auto;
    height: 130px;
    margin-top: 0px;
}

I have no idea why you want to put margin-top: 65px; (*_*). But it was the problem for you layout. Check it out soon.

Hope this can help you! have a nice day.
Quyen,

Hi again,

I just finished it, have look and I hope this could help you a bit (*_*).
Notes: In this database, I created a table called 'yum' , 2 fields are 'id' and 'name'. When you test this example, try to created a database has the same my database information. If not then you will get some errors. OR you can set the database name and table name in the code below.

DBObject.php

<?php 
    /* Created by Quyen. 11 July 2009
     *    Email : cuncon.it@gmail.com
     * DBObject.php
     */
     
    /* 
     * I just create a very simple class called DBObject 
     */
    class  DBObject{
        /////////////////////////////////////////////////
        // PRIVATE PROPERTIES  
        /////////////////////////////////////////////////
        private $hostname, $username, $password, $db_name, $con, $table_name;
        
    /**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        public function __construct($hostname,$username,$password,$db_name){
            $this->hostname = $hostname;
            $this->username = $username;
            $this->password = $password;
            $this->db_name = $db_name;
        }
        
    /**
       * Connect to MySQL.
       * @return void
       */
        public function connectToMySQL(){
            $this->con = mysql_connect($this->hostname,$this->username,$this->password);
            if($this->con){
                echo "<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( "<br>Could not connect to MySQL" . mysql_error() . ".<br />");
            }
        }
     /**
       * Select a database you will work with.
       * @return void
       */        
       public function selectDB(){
            $result = mysql_select_db($this->db_name,$this->con);
            if($result){
                echo "<br>Connected sucessfully to database ' ".$this->db_name." ' .<br><br>";
            }
            else{
                die( "<br>Could not connect to database ' ". $this->db_name. " '  ".mysql_error() . ".<br />");
            }
        }
     /**
       * Get back the name of database.
       * @return String $db_name
       */        
        public function getDBName(){
            return $this->db_name;
        }
        
     /**
       * Set table name that you want to select data from.
       * @return void
       */        
        public function setTableName($table_name){
            $this->table_name = $table_name;
        }        
        
     /**
       * Get back the name of a table
       * @return String $table_name
       */    
        public function getTableName(){
            return $this->table_name;
        }

     /**
       * View data of a table
       */    
        public function viewTableData($db_name,$table_name){
               mysql_select_db($db_name);
            $sql = "Select * from $table_name";
            $result = mysql_query($sql);
            $count_fields = mysql_num_fields($result); 
            $count_rows = mysql_num_rows($result);
            $index = 0;
            echo "
            <table border='1' width='50%'>
                <tr bgcolor='#CCCCCC'>
                    <td colspan='2'>This is '$table_name' table data</td>
                </tr>
                <tr bgcolor='#CCCCCC'>
                    <td>ID </td>
                    <td>Fruit name</td>
                </tr>
    
            ";
            while($row = mysql_fetch_array($result))
            {
            echo "
                <tr>
                    <td>".$row['id']."</td>
                    <td>".$row['name']."</td>
                </tr>";
            }
            echo "
            </table>
            "; 
        }
        
    /**
       * Show a simple message.
       * @return void
       */        
        public function showMessage(){
            echo "
                <br />
                I don't have much time, because I have something to do tomorrow, then if you
                have some time, read some more about MVC (Model - View - Control) to organize your 
                object better.For now I just put all them together. Work more on your database (*_*)....<br>
                Have a good time!<br>
            ";
        }
        
    }
?>
 

CreateObject.php

<?php 
    //Include DBObject.php file 
    include ("DBObject.php");
    
    //Create objects from DBObject class.
    $myObj = new DBObject("localhost","root","","mydb");
    
    //Call methods of objects.
    $myObj ->connectToMySQL();
    $myObj ->selectDB();
    
    $myObj ->setTableName("yum");
    $myObj ->viewTableData($myObj->getDBName(),$myObj->getTableName());
    $myObj ->showMessage();
?>

If you have any question, just email me at cuncon.it@gmail.com. or leave your reply in this forum. Goodnight.
Quyen,

russellharrower wrote:

hi Quyen,

I get what you are doing, but just one last question if you can help that would be great.
how to i fetch data from the database using oops?

like say i need to show the table called "yum"

inside the table "yum" it has two rows with data, under the row called "fruit"
1 - apple
2 - banana

But i when i run the select fruit from yum;
it prints both of the fruit - "applebanana"
what i need it to do is give each fruit a class or a way i can call it by

$class->fruitone;
$class->fruittwo;

Can you help there?

Did you mean that:
1. You want to select your data from table called 'yum'. This table has two fields: 'id' and 'fruit'.
2. You need to select all the records from the table above, and fill up them in a table? like

id      name

1       banana 
2       apple
3       warter melon
..       ...................

Is that what you mean?(*_*)
Quyen,

Hi,
You are welcome, i am not sure this will be a good example for you but I hope so.

DBObject.php

<?php 
    /* Created by Quyen. 11 July 2009
     *    Email : cuncon.it@gmail.com
     * DBObject.php
     */
     
    /* 
     * I just create a very simple class called DBObject 
     */
    class  DBObject{
        /////////////////////////////////////////////////
        // PRIVATE PROPERTIES  
        /////////////////////////////////////////////////
        private $hostname, $username, $password, $db_name, $con;
        
    /**
       * Constructor
       * @param String $hostname,$username,$password,$db_name. 
       * All information we need to provide whenever connect to db.
       */
        public function __construct($hostname,$username,$password,$db_name){
            $this->hostname = $hostname;
            $this->username = $username;
            $this->password = $password;
            $this->db_name = $db_name;
        }
        
    /**
       * Connect to MySQL.
       * @return void
       */
        public function connectToMySQL(){
            $this->con = mysql_connect($this->hostname,$this->username,$this->password);
            if($this->con){
                echo "<br>Connected sucessfully to MySQL .<br>";
            }
            else{
                die( "<br>Could not connect to MySQL" . mysql_error() . ".<br />");
            }
        }
    /**
       * Select a database you will work with.
       * @return void
       */        
       public function selectDB(){
            $result = mysql_select_db($this->db_name,$this->con);
            if($result){
                echo "<br>Connected sucessfully to database ' ".$this->db_name." ' .<br>";
            }
            else{
                die( "<br>Could not connect to database ' ". $this->db_name. " '  ".mysql_error() . ".<br />");
            }
        }
        
    /**
       * Later you can write some methods  to set and get value of Fields (Variables).
       * @
       */    
       public function setDBName()    {}
        
    /**
       * Show a simple message.
       * @return void
       */        
        public function showMessage(){
            echo "
                <br />Now you can execute any queries or create a database or do what ever you like (*_*)....<br>
                Have a good time!
            ";
        }
        
    }
?>

CreateObject.php

<?php 
    //Include DBObject.php file 
    include ("DBObject.php");
    
    //Create objects from DBObject class.
    $myObj = new DBObject("localhost","root","","mysql");
    $anotherObj = new DBObject("localhost","root","","information_schema");
    
    //Call methods of objects.
    $myObj ->connectToMySQL();
    $myObj ->selectDB();
    //$myObj ->showMessage();
    
    $anotherObj->connectToMySQL();
    $anotherObj ->selectDB();
    $anotherObj ->showMessage();
?>

Have a nice day!
Quyen,

russellharrower wrote:

Hi guys, I have looked everywhere with NO success, I don't understand any of these frameworks that are already out,
and i have read all the killerphp.com documents and watch all the videos that i can find about OOP and still cant find what i need.

I am new to OOP but know PHP, if someone can PLEASE help me out, and show me an example on how to use OOP to connect to MySQL that would be GREAT!

PLEASE HELP

Thanks

Hi, I just created an object today, If you like I can share it with you. My object just does something simple like connect to mysql, select any database that you want to work with. Let me know if you interested this.
Quyen,

25

(6 replies, posted in PHP)

Hi,
You know I don't speak English well, I was replying to Akurtula, and I just tried to give him some of my ideas. Sorry if I didn't explain it that well.