Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

news system

Recommended Posts

hey guys,

just started making a news system. What I'm trying to do is being able to tell the script how many "stories" I want and it displays the appropriate number of input boxes.

so for example, I need 3 stories it will automaticly make 3 input boxes, if I need 5 it will make 5 input boxes, etc. I'd also like to be able to have it on the same form, without having to create 2 seperate forms.

Any ideas on how I can do this?

Share this post


Link to post
Share on other sites
Here's one way. I wrote this quickly -- i.e. no error checking, no code to process the stories.
[code]<html>
<head>
    <title></title>
    <style>
    .lbl {
        display: block;
        width: 15%;
        float: left;
        font-weight: bold;
    }
    .bx {
        display: block;
        width: 60%;
    }
    .txtinp {
        width: 99%;
    }
    </style>
</head>

<body>
<?php
    $tmp = array();
    $tmp[] = '<form method="post">';
    if (!isset($_POST['num_stories'])) {
        $tmp[] = 'Number of stories: <select name="num_stories">';
        for($i=1;$i<=10;$i++)
            $tmp[] = '<option value="' . $i . '">' . $i . '</option>';
        $tmp[] = '</select>';
        $tmp[] = '<br><input type="submit" name="submit" value="Set number of stories">'; }
    else {
        for ($i=1;$i<=$_POST['num_stories'];$i++) {
            $tmp[] = '<span class="lbl">Story #' . $i . ' Title:</span><span class="bx"><input type="text" class="txtinp" name="story_title[' . $i . ']"></span><br>';
            $tmp[] = '<span class="lbl">Story #' . $i . ' Content:</span><span class="bx"><textarea class="txtinp" rows=10 name="story_content[' . $i . ']"></textarea></span><br>';
            $tmp[] = '<hr>'; }
        $tmp[] = '<input type="submit" name="submit" value="Send Stories">';
    }
    $tmp[] = '</form>';
    echo implode("\n",$tmp)."\n";
    ?>

</body>
</html>[/code]
The style section is there to format the form inputs better...

Ken

Share this post


Link to post
Share on other sites
thanks Ken, after a few mods it works perfectly for what I need.

There's only one problem though, I need to add a hidden field but in the 2nd part of the form because it submits to admin.php?a=newnews and if the hidden field = yes the admin.php processes it

I tried placing an echo with the hidden process but that didn't work so whenever it submits it just goes back and asks how many stories..

Any ideas what I can change?

[code]
<?php
    $tmp = array();
    $tmp[] = '<form action="admin.php?a=newnews" method="post" name="form" id="form">';
    if (!isset($_POST['num_news'])) {
        $tmp[] = 'Number of news: <select name="num_news">';
        for($i=1;$i<=10;$i++)
            $tmp[] = '<option value="' . $i . '">' . $i . '</option>';
        $tmp[] = '</select>';
        $tmp[] = '<br><br><input type="submit" name="submit" value="Set number of news">'; }
    else {
        echo '<input type="hidden" name="process" value="yes" />';
        for ($i=1;$i<=$_POST['num_news'];$i++) {
            $tmp[] = '<span class="lbl">News #' . $i . '<br><br> Upload Pic:</span><br><br><span class="bx"><input type="file" class="txtinp" name="file[' . $i . ']"></span><br>';
            $tmp[] = '<span class="lbl">Content:</span><span class="bx"><textarea class="txtinp" rows=10 cols=60 name="news_content[' . $i . ']"></textarea></span><br>';
            $tmp[] = '<hr>'; }
        $tmp[] = '<input type="submit" name="submit" value="Submit News">';
    }
    $tmp[] = '</form>';
    echo implode("\n",$tmp)."\n";
    ?>
[/code]

Share this post


Link to post
Share on other sites
You'll notice that I put all of the HTML into a temporary array and then echo everything out at the end, so the line you put in actually got output before the form and had no affect.

change this line:
[code]<?php echo '<input type="hidden" name="process" value="yes" />'; ?>[/code]
to
[code]<?php $tmp[] = '<input type="hidden" name="process" value="yes" />'; ?>[/code]

Ken

Share this post


Link to post
Share on other sites
one more thing, I can't seem to process the form...

[code]
qtmp = array();
            foreach($_POST as $k => $v)
                switch($k) {
                    case 'news_content':
                        if (trim(stripslashes($v)) != '')
                            $qtmp[] = "content ='" . mysql_real_escape_string(trim(stripslashes($v))) . "'";
                    break;
                    
         }
[/code]

how can I collect all the info if there's more than 1 story and would it be better to store each story in it's own row, linked by their date or put everything in the same row and just seperate them with | or something like that?

Share this post


Link to post
Share on other sites
ok I think I found how

[code]
$num_news = $_POST['num_news'];
$qtmp = array();
            for ($i=1;$i<=$num_news;$i++) {
                $qtmp[] = "content ='" . mysql_real_escape_string(trim(stripslashes($_POST['news_content'.$i.'']))) . "'";
                $query = "INSERT INTO ".$prefix."news set " . implode(', ',$qtmp);
                  $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
            }
[/code]

but it's giving me a database error but "content" exists so I don't know what's the problem

Share this post


Link to post
Share on other sites
Whenn you use this:
[code]<?php
$qtmp = array();
            for ($i=1;$i<=$num_news;$i++) {
                $qtmp[] = "content ='" . mysql_real_escape_string(trim(stripslashes($_POST['news_content'.$i.'']))) . "'";
                $query = "INSERT INTO ".$prefix."news set " . implode(', ',$qtmp);
?>[/code]
to create your query, you will get something like this if you have more than one news story:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]yourtable[/color] SET content [color=orange]=[/color] [color=red]'story 1'[/color], content [color=orange]=[/color] [color=red]'story 2'[/color] [!--sql2--][/div][!--sql3--]
Which is incorrect and not what you want. You want one entry per story.

If you can post the all the code you currently have, it would be helpful.

Ken

Share this post


Link to post
Share on other sites
sure thing

admin
sure thing

admin.php
it's not letting me post the part that uploads the images....
[code]
case "newnews";
        $process=$_POST['process'];
        $num_news = $_POST['num_news'];
        if ($process == "yes") {
        $today = date("Ymd");
    
                 
        $result=mysql_query("SELECT * FROM ".$prefix."news WHERE date='$today") or die(query_error());
        $row=mysql_fetch_array($result);
          if ($row['id'] = "") {
          $tmpl->add_template("news_no");
          }
          else {
              $qtmp = array();
            for ($i=1;$i<=$num_news;$i++) {
                $qtmp[] = "content ='" . mysql_real_escape_string(trim(stripslashes($_POST['news_content'.$i.'']))) . "'";
                $query = "INSERT INTO ".$prefix."news set " . implode(', ',$qtmp);
                  $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
            }
          $tmpl->add_template("news_success");
          }
        }
        else {
        $tmpl->add_template("news_form");
        }
      break;

[/code]

and news_form.php
[code]
<div id="article">
<style>
    .lbl {
        display: block;
        width: 35%;
        float: left;
        font-weight: bold;
    }
    .bx {
        display: block;
        width: 80%;
    }
    .txtinp {
        width: 100%;
    }
    </style>

<?php
    $tmp = array();
    $tmp[] = '<form action="admin.php?a=newnews" method="post" name="form" id="form">';
    if (!isset($_POST['num_news'])) {
        $tmp[] = 'Number of news: <select name="num_news">';
        for($i=1;$i<=10;$i++)
            $tmp[] = '<option value="' . $i . '">' . $i . '</option>';
        $tmp[] = '</select>';
        $tmp[] = '<br><br><input type="submit" name="submit" value="Set number of news">'; }
    else {
        $tmp[] = '<input type="hidden" name="process" value="yes" />';
        $tmp[] = '<input type="hidden" name="size_limit" value="500" />';
        for ($i=1;$i<=$_POST['num_news'];$i++) {
            $tmp[] = '<span class="lbl">News #' . $i . '<br><br> Upload Pic:</span><br><br><span class="bx"><input type="file" class="txtinp" name="file[' . $i . ']"></span><br>';
            $tmp[] = '<span class="lbl">Content:</span><span class="bx"><textarea class="txtinp" rows=10 cols=60 name="news_content[' . $i . ']"></textarea></span><br>';
            $tmp[] = '<hr>'; }
        $tmp[] = '<input type="submit" name="submit" value="Submit News">';
    }
    $tmp[] = '</form>';
    echo implode("\n",$tmp)."\n";
    ?>
</div>
[/code]

Share this post


Link to post
Share on other sites
got an idea about what I can do Ken? I've been trying to figure this out but haven't been succesful so far...

Share this post


Link to post
Share on other sites
alright tried everything I can think of but still can't get this to work.... can anyone please help me out? Would really appreciate it

Share this post


Link to post
Share on other sites
As far as I know, your form enctype needs to be specified as "multipart/form-data" in order to post binary data. See the example below, you can always google it to find out more.

[code]
<form enctype="multipart/form-data" action="scripts/upload.php" method="post" name="upload_form">
[/code]

Graham

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.