1

I am writing a basic CMS system and have come across something which should be seemingly simple -but is beginning to frustrate me.!

I am trying to pass an array through a select option field to populate a list of categories in which I can save a post.

I have a 'posts' form which comprises of 3 fields. Title, content and Category ID (CatID).

When the user creates a post, they can select the category they wish to assign the post assigned to by using a drop down list - (this is populated by using a different form).

So the technical bit; -

MySQL DB:-

categories = catname (char60 PRIMARY), catid (INT10, AI) posts = id (bigint20 PRIMARY), catid (int10 PRIMARY), title (text), content (varchar255)

Example of categories populates: catname = Home / catid = 1 ...etc

Output.php ;

<?php
function display_post_form($post = '') {
$edit = is_array($post);
?>
    <form action="<?php echo $edit ? 'edit.php' : 'add.php' ; ?>" method="post">
    <table border="0">
    <tr>
    <td> Title:</td>
        <td> <input type="text" name="title" value="<?php echo $edit ? $post['title'] : '' ; ?>" size="60" /> </td>
    </tr><tr>
    <td> Content:</td>
    <td> <textarea id="editor1" name="content" value="<?php echo $edit ? $post['content'] : '' ; ?>"> </textarea> </td>
    </tr><tr>
    <td> Category:</td>
    <td><select name="catid">
    <?php
            $cat_array = get_categories($catid, $catname);
        foreach($cat_array as $thiscat) {
        echo "<option value=\"".$thiscat['catid']."\" ";
            if (($edit) && ($thiscat['catid'] == $post['catid'])) {
            echo " selected";
        }
        echo ">".$thiscat['catname']."</option>";
        }
    ?>
    </select>
    </td>
    </tr><tr>
    <td> Button:</td>
    <td <?php if (!$edit) { echo "colspan=2"; } ?> align="center">
    <?php
            if ($edit)
        echo "<input type=\"hidden\" name=\"_id\" value=\"". $post['id'] ."\" />";
    ?>
    <input type="submit" value="<?php echo $edit ? 'Update' : 'Add' ; ?> Post" />
    </form></td>
        </td>
        </tr>
    </table>
</form>
<?php
}
?>

Functions.php ;

function get_categories($catid, $catname) {

$conn = db_connect();

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL "  .mysqli_connect_error();
}

$sql = "SELECT catname, catid FROM categories";

$result = mysqli_query($conn, $sql) or die(" Could not query database");

    while($row = mysqli_fetch_assoc($result)) {
    printf("\n %s %s |\n",$row["catname"],$row["catid"]);
}

mysqli_close($conn);

}

I am able to call in the 'get_cattegories()' function which generates a flat data of categories and their respective id's. I then combined this with the Select Option Field in the Output.php file and it doesn't generate anything.

Can anyone give some useful tips or advice? Many thanks :)

1
  • Why are you putting parameters in get_gategories function? Where are you returning anything from that function? Why don't you just put catname and catid in an array and return it? Commented Mar 20, 2014 at 0:06

1 Answer 1

2

You are not returning the array but printing a string to the output. Change printf to return:

function get_categories($catid, $catname) {

$conn = db_connect();

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL "  .mysqli_connect_error();
}

$sql = "SELECT catname, catid FROM categories";

$result = mysqli_query($conn, $sql) or die(" Could not query database");
$categories = array();
    while($row = mysqli_fetch_assoc($result)) {
    $categories[] = $row;
}

mysqli_close($conn);

return $categories;

}

Also I agree for the comments to your question. The arguments are useless.

You also may refactor the code, actually... alot. Move the mysql_connect() to the other place, probably at the beginning of your script.

I suggest to use some frameworks. I think KohanaPHP will be a good start. You will learn about architecture and some design patterns. Keep the good work and improve your skills ;-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.