Jump to content

How do i Select this?


billy_111

Recommended Posts

Hi,

 

I'm not quite sure how to do this, so i thought i'd ask you guys from some assistance.

 

Basically i am inserting Author's into a table successfully using an array. The reason for this is that i have multiple authors being added and there is no limit as to how many. An example of what i mean can be seen here:

 

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/test.html

 

You can click on "Add author" to add however many necessary..

 

Anyway, i have got the insert working, however when i edit i want to be able to see all the authors that have been added but obviously i don't know how many there are..

 

Typically i would like to see something like this:

 

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/edit.jpg

 

So i would click on an edit link and it would pre populate the text boxes. I don't have a problem with doing this, but how can i show the correct amount of input textboxes based on how many authors exist for that specific record?

 

Can someone offer some advice please?

 

The input elements are like so:

 

<input type="text" name="author[]" id="author1"/>

<input type="text" name="author[]" id="author2"/>

<input type="text" name="author[]" id="author3"/>

...

...

...

<input type="text" name="author[]" id="author10"/>

 

Some records may have 1 author some may have 10, so how can i do this?

 

Thanks again..

Link to comment
Share on other sites

Ok, my table structure is as follows:

 

CREATE TABLE IF NOT EXISTS `People` (

  `Pid` int(11) NOT NULL auto_increment,

  `Pname` varchar(255) NOT NULL,

  PRIMARY KEY  (`Pid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

 

CREATE TABLE IF NOT EXISTS `PeopleCon` (

  `PCid` int(5) NOT NULL auto_increment,

  `Pid` int(5) NOT NULL,

  `PCorder` int(2) NOT NULL,

  `PCdateadded` datetime NOT NULL,

  `PCdeleted` int(1) NOT NULL,

  PRIMARY KEY  (`PCid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

 

Then my method is as follows:

 

    public function selectByHidAndPid($PCHid, $PaperID){

        $query = "SELECT Pid Pname FROM People p
                  INNER JOIN PeopleCon c ON p.Pid = c.Pid
                  WHERE c.PCHid = ".$PCHid." AND c.Paper_ID = ".$PaperID;
        return mysql_query($query);
    }

 

On my page i call the method like so:

 

if(isset($_GET['ID']) && is_numeric($_GET['ID'])):
    $people = new People();
    $authors = $people->selectByHidAndPid($_GET['CPRid'], $_GET['ID']);
endif;

 

My input's are like so:

 

<td><input type="text" name="author[]" id="author1" value=""/>
<td><input type="text" name="author[]" id="author2" value=""/>
<td><input type="text" name="author[]" id="author3" value=""/>
...
...
<td><input type="text" name="author[]" id="author10" value=""/>

 

So i need some sort of while or for loop, i just don't know how it should be written, i was thinking something like:

 

    while($row = mysql_fetch_array($authors)):
        
    endwhile;

 

But then i'm not sure where to go from here?

 

Can anyone help?

 

Thanks

Link to comment
Share on other sites

Sorry the syntax for my method was wrong it should be:

 

    public function selectByHidAndPid($PCHid, $PaperID){

        $query = "SELECT c.Pid, p.Pname FROM People p
                  INNER JOIN PeopleCon c ON p.Pid = c.Person_ID
                  WHERE c.PCHid = ".$PCHid." AND c.Pid = ".$PaperID;
        return mysql_query($query);
    }

 

And table structure is as follows:

 

CREATE TABLE IF NOT EXISTS `People` (

  `Pid` int(11) NOT NULL auto_increment,

  `Pname` varchar(255) NOT NULL,

  PRIMARY KEY  (`Pid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

 

CREATE TABLE IF NOT EXISTS `PeopleCon` (

  `PCid` int(5) NOT NULL auto_increment,

  `Person_ID` int(5) NOT NULL,

  `PCHid` int(5) NOT NULL,

  `Pid` int(5) NOT NULL,

  `PCorder` int(2) NOT NULL,

  `PCdateadded` datetime NOT NULL,

  `PCdeleted` int(1) NOT NULL,

  PRIMARY KEY  (`PCid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Link to comment
Share on other sites

if (mysql_num_rows($authors) == 0) 
{
    echo "No rows found, nothing to print ";
}
else 
{
  $count = 0;
  while ($row = mysql_fetch_assoc($authors)) 
  {
    $count++;
    //echo $row["Pid"]; 
    //echo $row["Pname"];
    echo "<td><input type=\"text\" name=\"author[]\" id=\"author$count\" value=\".$row['Pid']."\"/>";
  }
  
  mysql_free_result($authors);
}  

Link to comment
Share on other sites

<?php

if(isset($_GET['ID']) && is_numeric($_GET['ID']))
{
    $people = new People();
    $result = $people->selectByHidAndPid($_GET['CPRid'], $_GET['ID']);
?>   
<form action="" method="post">
<?php 
    $i = 1;
    while($row = mysql_fetch_assoc($result)): ?>
    
  <input type="text" name="author[]" id="author<?php echo $i++; ?>" value="<?php echo $row['Pname']; ?>" /><br />

<?php
    endwhile; ?>
    <input type="submit" name="submit" value="submit" />
</form>  
<?php 
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.