Jump to content


Photo

news system


  • Please log in to reply
12 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 June 2006 - 09:12 PM

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?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 June 2006 - 09:48 PM

Here's one way. I wrote this quickly -- i.e. no error checking, no code to process the stories.
<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>
The style section is there to format the form inputs better...

Ken

#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 June 2006 - 04:36 AM

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?

<?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";
    ?>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 06:02 AM

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:
<?php echo '<input type="hidden" name="process" value="yes" />'; ?>
to
<?php $tmp[] = '<input type="hidden" name="process" value="yes" />'; ?>

Ken

#5 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 June 2006 - 06:26 AM

thank you very much Ken :)

I honestly don't know why I missed that :P

#6 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 June 2006 - 09:11 PM

one more thing, I can't seem to process the form...

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;
                    
         }

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?

#7 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 June 2006 - 10:27 PM

ok I think I found how

$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());
            }

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

#8 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 11 June 2006 - 03:50 PM

anyone?

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 June 2006 - 04:37 PM

Whenn you use this:
<?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);
?>
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] INTO yourtable SET content = 'story 1', content = 'story 2' [!--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

#10 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 11 June 2006 - 05:37 PM

sure thing

admin
sure thing

admin.php
it's not letting me post the part that uploads the images....
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;


and news_form.php
<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>


#11 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 12 June 2006 - 12:55 PM

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

#12 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 14 June 2006 - 05:21 AM

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

#13 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 14 June 2006 - 09:11 AM

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.

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

Graham




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users