Re: Getting Array to Display

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21 April 2010 16:01, Gary <gwp@xxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxxx> wrote in message
> news:1271858245.20937.10.camel@xxxxxxxxxxxx
>>
>> 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
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

As you are using <input type="checkbox" name="keyword[]" value="fox"
/>, then you are creating an array called $_POST['keyword'].

So far so good.

When you construct your SQL statement, you are trying to put the array
into a string ...

Unfortunately, an handling an array like that will result in the array
becoming the text ...

Array

e.g.

<?php
$a = array(1,2,3);
$b = "$a $a $a";
echo $b;
?>

outputs ...

Array Array Array


So, either you need to store the keywords in a separate table and link
it to the main table by some sort of ID, or convert the array to a
string.

You could use something like ...

$keywords = implode("\t", $_POST['keyword']);

to produce a tab separated list of keywords.

You could use a comma (',') as the separator, but if any keyword
contains a comma (unlikely, but thinking ahead, keywords COULD be
keyphrases), then the comma becomes a separator on the way out.

Or you could serialize the array if you weren't interested in
processing it in the DB.

$keywords = serialize($_POST['keyword']);


On the way out of the DB, you would use ...

$keywords = explode('\t', $row['keywords']);
or
$keywords = unserialize($row['keywords']);



-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux