Re: Getting Array to Display
| From: | Ashley Sheridan | Date: | Thu, 22 Apr 2010 09:57:22 +0000 |
| Subject: | Re: Getting Array to Display | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304406@lists.php.net to get a copy of this message | ||
On Wed, 2010-04-21 at 14:31 -0400, Gary wrote:
> Ashley
>
> If I were to create a child table, would the Type be a Bool?
>
> Gary
>
>
> "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message
> news:1271862971.20937.19.camel@localhost...
> > On Wed, 2010-04-21 at 11:01 -0400, Gary wrote:
> >
> >> Ashley
> >>
> >> Thank you for your reply.
> >>
> >> I was trying to get the structure of the DB into a text form, cant seem
> >> to
> >> find that (phpmyadmin).
> >>
> >> This is the insert code
> >>
> >> <?php
> >> $caption=($_POST['caption']);
> >> $keyword=($_POST['keyword']);
> >> $description=($_POST['description']);
> >> $image_file=($_POST['image_file']);
> >> $where_taken=($_POST['where_taken']);
> >>
> >> $dbc=mysqli_connect('', '', '', 'images');
> >>
> >> $query="INSERT INTO images(caption, where_taken, keyword, description,
> >> image_file) VALUES
> >>
> >> ('$caption','$where_taken','$keyword','$description','$image_file')";
> >> if (isset($_POST['submit'])) {
> >>
> >> $how_many = count($keyword);
> >> echo 'keywords chosen: '.$how_many.'<br />';
> >> if ($how_many>0) {
> >> echo 'You chose the following keywords:<br />';
> >> }
> >> for ($i=0; $i<$how_many; $i++) {
> >> echo ($i+1) . '- ' . $keyword[$i] . '<br />';
> >> }
> >> echo "<br />";
> >> }
> >> $result = mysqli_query($dbc, $query)
> >> or die('Error querying database.');
> >>
> >> if($result == true) {
> >> echo "Successfully Inserted $image_file into database";
> >> } else {
> >> echo "Some Error Occured While Inserting Records";
> >> }
> >>
> >> mysqli_close($dbc);
> >> ?>
> >>
> >> I hope that helps, and again thank you for your reply.
> >>
> >> gary
> >>
> >>
> >> "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message
> >> news:1271858245.20937.10.camel@localhost...
> >> > On Wed, 2010-04-21 at 09:47 -0400, Gary wrote:
> >> >
> >> >> I have a form that I have a (ever growing) list of checkboxes, Here
> >> >> is a
> >> >> sample of the code for it.
> >> >>
> >> >> <input name="keyword[]" type="checkbox"
> >> >> value="fox" />
> >> >>
> >> >> It seems to go in, when I say seems to, I get a result of Array in the
> >> >> table, the code is listed below. I have tried various solutions I
> >> >> found
> >> >> in
> >> >> searching the issue, but have only been able to so far get Array.
> >> >>
> >> >> echo '<table border="1"><th>Id
> >> >> Number</th><th>Date
> >> >> Entered</th><th>Caption</th><th>Where
> >> >>
> >> >> Taken</th><th>Keywords</th><th>Description</th><th>Image</th>';
> >> >> while ($row = mysqli_fetch_array($data)) {
> >> >>
> >> >> echo '<tr><td>' . $row['image_id'].
> >> >> '</td>';
> >> >> echo '<td>' . $row['submitted'].
> >> >> '</td>';
> >> >> echo '<td>' . $row['caption'].
> >> >> '</td>';
> >> >> echo '<td>' . $row['where_taken'] .
> >> >> '</td>';
> >> >> echo '<td>' . $row['keyword'].
> >> >> '</td>';
> >> >> echo '<td>' . $row['description'] .
> >> >> '</td>';
> >> >> if (is_file($row['image_file'])) {
> >> >> echo '<td><img
> >> >> src="'.$row['image_file'].'" width="100px"
> >> >> height="100px"/></td>';
> >> >> }
> >> >>
> >> >> As a bonus question, does anyone have any idea why the image would
> >> >> show
> >> >> up
> >> >> in IE8, and not FF?
> >> >>
> >> >> Thanks for your help.
> >> >>
> >> >> Gary
> >> >>
> >> >>
> >> >>
> >> >> __________ Information from ESET Smart Security, version of virus
> >> >> signature database 5047 (20100421) __________
> >> >>
> >> >> The message was checked by ESET Smart Security.
> >> >>
> >> >> http://www.eset.com
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >
> >> >
> >> > You say you're getting an entry of Array in your table. Is this after
> >> > performing an insert query from the form? If so, we're gonna need to
> >> > see
> >> > the code for that! If it's that you're retrieving from the DB and
> >> > getting Array as the output, then it could be that you're using string
> >> > functions on an array. However, as you said images are displaying, I
> >> > don't think that's the case. Maybe some of the fields in the DB have a
> >> > string literal of 'Array' as their value?
> >> >
> >> > As for the images not displaying correctly, have you tried to just open
> >> > those images up in a browser using their uri? Just copy and paste it
> >> > from the <img> tag. It might give more input on exactly what is
> >> > happening with the image.
> >> >
> >> > Thanks,
> >> > Ash
> >> > http://www.ashleysheridan.co.uk
> >> >
> >> >
> >> >
> >> >
> >> >
> >> > __________ Information from ESET NOD32 Antivirus, version of virus
> >> > signature database 5047 (20100421) __________
> >> >
> >> > The message was checked by ESET NOD32 Antivirus.
> >> >
> >> > http://www.eset.com
> >> >
> >> >
> >>
> >>
> >> __________ Information from ESET NOD32 Antivirus, version of virus
> >> signature database 5047 (20100421) __________
> >>
> >> The message was checked by ESET NOD32 Antivirus.
> >>
> >> http://www.eset.com
> >>
> >>
> >>
> >>
> >
> >
> > You're inserting the string 'Array' into the DB. The form you have is
> > sending an actual array, but the insert statement can only insert a
> > single string. The string of the array is, as you've found out, 'Array'.
> >
> > Consider normalising the DB and having a keywords table with 3 fields:
> > id, image_id, keyword.
> >
> > Then each entry in the keywords table would be a keyword, linked by
> > image_id. You'll also need to change your images table to include an id
> > by which you can reference each image individually and index by.
> >
> > Lastly, be very careful of your insert statements. The way things stand,
> > it would be easy for someone to maliciously enter their own statements.
> > Use something like mysql_real_escape_string() to protect against MySQL
> > attacks.
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> >
> >
> >
> > __________ Information from ESET NOD32 Antivirus, version of virus
> > signature database 5047 (20100421) __________
> >
> > The message was checked by ESET NOD32 Antivirus.
> >
> > http://www.eset.com
> >
> >
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus signature database 5048
> (20100421) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>
>
The table won't have a type, it will just be another regular table.
The second table has 3 fields, id, image_id, keyword. The image_id field
will match up against the image_id row in the first table, so you can
have multiple rows in the keywords table that match up to just one row
in the images table. This is called a one-to-many relationship.
Then, you would do a typical SQL join to retrieve the data:
SELECT images.image_id, images.submitted, images.caption,
images.where_taken, images.description, images.file, keywords.keyword
FROM images
LEFT JOIN keywords ON (keywords.image_id = images.image_id)
WHERE images.image_id = x
and change x to whatever identifier you are using. This will retrieve a
bunch of rows for the keywords for each image.
Thanks,
Ash
http://www.ashleysheridan.co.uk