Jump to content

MYSQL>PHP displaying help.


Braveheartt

Recommended Posts

Hey there. I'm trying to display information gathered from a HTML form which is stored into a MYSQL table.

 

Here is the displaying code:

 

<?php require("menu.php");	

//Connect to MYSQL server
mysql_connect("Localhost", "brave11", "police") or die(mysql_error());
   //echo "<center><font color='white'>Successfully connected to MySQL!</font></center><br />"; 

//Connect to MYSQL database   
mysql_select_db("brave11_bugs") or die(mysql_error());
   echo "<center><font color='white'><h4>Successfully connected to the Bugs Database!</h4></font></center>";
   
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM bugs")
or die(mysql_error());  

// store the record of the "example" table into $row
echo "<CENTER><TABLE BORDER='2' BORDERCOLOR='#336699' CELLPADDING='2' BGCOLOR='white' CELLSPACING='2' WIDTH='100%'></CENTER>";
echo "<tr> <th>Poster</th> <th>Bug Description</th> <th>Screenshot 1</th> <th>Screenshot 2</th> <th>Screenshot 3</th> <th>Bug Type</th> <th>Priority</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['poster'];
echo "</td><td>"; 
echo $row['description'];
echo "</td><td>"; 
echo "<IMG SRC='$row['screen1']' BORDER='0'>";
echo "</td><td>";  
echo $row['screen2'];
echo "</td><td>";
echo $row['screen3'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['priority'];
echo "</td></tr>";
} 

echo "</TABLE>";
?></font></center></body></html>

 

 

Problems:

 

1. I want the "screen1 2 and 3" to display as images, so I did the following:

echo "<IMG SRC='$row['screen1']' BORDER='0'>";

 

But that doesn't work! Why? How can I fix it? Error:

 Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/brave11/public_html/home/bugs/bugs_main.php on line 26

 

 

2. The above creates a new HTML table EVERY time the script is ran (after each time the data is taken into the MYSQL table) how can I get that table creation to run just once, creating ONE table and the set amount of colloms once but allowing the rows to be created each time new data is received?

 

So basically, every time a new form is submitted a new set of rows are made and the new data input.

 

3. I want the "pri_lvl aka priority" to display in the HTML table as a certain colour depending on which radio button (urgent, medium or low) is pressed and stored. How can I achieve that?

Link to comment
Share on other sites

1st

echo "<IMG SRC='$row[screen1]' BORDER='0'>";

You will need to split the strings and concatinate them:

echo "<IMG SRC='" . $row[screen1] . "' BORDER='0'>";

Both are correct however there are few notes you should consider.:

 

1. When using associative arrays always wrap the key within quotes, eg: $arr['key'] not $arr[key] although both return the same result they are in fact completely different. If you do not wrap keys within quotes PHP will think you are using a Constant rather than string, and so if you have error_reporting set high enough you may find PHP displaying the following notice don your page:

Notice: Use of undefined constant key - assumed 'key'

 

2. If you are using an array within a string which is an associative array wrap the variable within curly braces:

echo "<b>{$arr['key']}</b>"

Otherwise you may get the following error message:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Link to comment
Share on other sites

look

<?php
// Let's show all errors
error_reporting(E_ALL);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Correct
print $arr['fruit'];  // apple
print $arr['veggie']; // carrot

// Incorrect.  This works but also throws a PHP error of
// level E_NOTICE because of an undefined constant named fruit
// 
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit];    // apple

// Let's define a constant to demonstrate what's going on.  We
// will assign value 'veggie' to a constant named fruit.
define('fruit', 'veggie');

// Notice the difference now
print $arr['fruit'];  // apple
print $arr[fruit];    // carrot

// The following is okay as it's inside a string.  Constants are not
// looked for within strings so no E_NOTICE error here
print "Hello $arr[fruit]";      // Hello apple

// With one exception, braces surrounding arrays within strings
// allows constants to be looked for
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

// This will not work, results in a parse error such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>

from http://www.php.net/manual/en/language.types.array.php

Link to comment
Share on other sites

Woops, thanks for catching that, wildteen88, I didn't mean to leave the quotes out, I just coppied what sasa wrote to edit. Even seeing sasa's newest post, I would still recommend always using a string for the subscript when the key is, in fact, a string.

Link to comment
Share on other sites

Thanks all!!

 

Any word on the question 3? Ignore question 2, correcting my code fixed that problem!

 

I also have another question to throw out there. I've made a new "ID" field in my HTML table, how could I get PHP to start from 1 and keep adding 1 for each new entry?

 

echo $row...?

Link to comment
Share on other sites

3. I want the "pri_lvl aka priority" to display in the HTML table as a certain colour depending on which radio button (urgent, medium or low) is pressed and stored. How can I achieve that?

 

Provided $row['priority'] holds either urgent, medium or low then do something like this:

 

Add the following line:

$colors = array('urgent' => 'red', 'medium' => 'orange', 'low' => 'blue');

Before:

// store the record of the "example" table into $row
echo "<CENTER><TABLE BORDER='2' BORDERCOLOR='#336699' CELLPADDING='2' BGCOLOR='white' CELLSPACING='2' WIDTH='100%'></CENTER>";

 

and change:

	echo "</td><td>";
echo $row['priority'];
echo "</td></tr>";

 

to:

	echo '</td><td  style="font-weight:bold; color:' . $colors[strtolower($row['priority'])] . ';">';
echo '<span' . $row['priority'] . '</spam>';
echo "</td></tr>";

Link to comment
Share on other sites

Thanks! That looks really complicated!!

 

I managed to do it like so:

 

if($row['priority'] == Urgent)
   echo "<strong><font color='#FF0000'>Urgent</font></strong>";
elseif($row['priority'] == Medium)   
    echo "<strong><font color='#FF6600'>Medium</font></strong>";	
 elseif($row['priority'] == Low)   
    echo "<strong><font color='#FFCC00'>Low</font></strong>";

 

 

I'm still trying to get my ID box to start with 0 and rise at increments of 1 per row. I've tried:

 

$id = 0;
$ida = $id += 1;

 

Then echoing $ida, but that obviously made them all 1!!

 

 

Also. I'm trying to get the text to wrap in a certain size of table, for example if you did "ttttttttttttttt..." it would throw the whole table off! So I tried a textarea to solve the problem:

 

echo "<TEXTAREA wrap='physical' name='des' rows=5 cols=60 COLOR='#FFFFFF' MAXLENGTH=9999>".$row['description']."</TEXTAREA>";

 

 

But of course that is editable which is the first no no! Secondly the background will always be white and the text black. But it worked fine! How could I do the exact same (text wrap, scroller etc) but with my chosen background and font colour and of course make it non-editable?

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.