Forum Moderators: coopster

Message Too Old, No Replies

Passing mysqli variables to xhtml

Interfacing php variables to xhtml

         

calmseas

2:04 am on Aug 20, 2010 (gmt 0)

10+ Year Member



I'm another mysqli newbie to php.

My objective is to retrieve data from a MySql DB and then format and display it in xhtml. In the following php snippet, I've retrieved the contents of
$course_title[1]; but the contents don't get printed out in xhtml and therefore would be unavailable for formatting. How do I interface php variable contents to html?

for ( $i=0 ; $i < $num_rows ; $i++ )
{
$row = @mysqli_fetch_assoc( $result ) ;
echo "<br />" ;
$course_title[$i] = $row['course_title'] ;
echo $course_title[$i] ;
echo "<br />" ;
echo $row[ 'course_title' ] ;
echo "<br />" ;
echo $row['last'] ;
echo "<br />" ;
echo $row['first'] ;
}

@mysqli_free_result( $result ) ;
@mysqli_close( $db ) ;
?>
Hello World
<br>
$course_title[1]

rocknbil

3:28 am on Aug 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard calmseas,

for ( $i=0 ; $i < $num_rows ; $i++ )
{
$row = @mysqli_fetch_assoc( $result ) ;

I can only presume this is the result of mysql_num_rows. You do not need this, I wish I knew who started this. :-) Remove that for loop entirely, and see if this does anything.

<?php
// connection, select, and mysqli_query assumed
//
while ($row = mysqli_fetch_assoc($result)) {
echo "<br />" . $row['course_title'] .
"<br />" . $row['last'] .
"<br />" . $row['first'];
}
mysqli_free_result($result);
mysqli_close($db);
?>

You don't need XHTML here, it's all plain html anyway, but have given up on that one. :-) See if that outputs for you.

calmseas

6:24 am on Aug 20, 2010 (gmt 0)

10+ Year Member



rocknbil -
You are correct, I would have eventually ended up using the WHILE construct. However, I failed to adequately explain my question. Will try again:
I want to be able to use the results of $result from within xhtml and am trying to figure out how to pass the variables from php to html.
I'm converting Java Server Pages code to php. Here is a snippet of JSP code:
<c:forEach items="${userDbInfo.rows}" var="row">
<tr>
<td>
<form action="../course_registration/delete_course_signups.jsp" method="post">
<input type="hidden" name="lastName"
value="${fn:escapeXml(row.LASTNAME)}">
<input type="hidden" name="firstName"
value="${fn:escapeXml(row.FIRSTNAME)}">
<input type="hidden" name="courseTitle"
value="${fn:escapeXml(row.COURSE_TITLE)}">
<input type="submit" value="Delete">
</form>
</td>
<td>${fn:escapeXml(row.LASTNAME)}</td>
<td>${fn:escapeXml(row.FIRSTNAME)}</td>
<td>${fn:escapeXml(row.EMAIL_PHONE)}</td>
<td>${fn:escapeXml(row.COURSE_TITLE)}</td>
<td>${fn:escapeXml(row.SIGNUP_DATE)}</td>
</tr>
</c:forEach>
In this case; e.g. I'm accessing row.COURSE_TITLE, which was SELECT'd out of an Oracle DB.
Equivalently, how do I get at e.g. $row['last'] returned out of MySQL from within html?
I hope this makes sense. The biggest problem is always defining the question. Appreciate your help.

rocknbil

4:02 pm on Aug 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



**Normally** the PHP approach to that would be something like this.

<?php // start loop
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td>
<form action="/course_registration/delete_course_signups.jsp" method="post">
<input type="hidden" name="lastName"
value="<?php echo $row['LASTNAME']; ?>">
<input type="hidden" name="firstName"
value="<?php echo $row['FIRSTNAME']; ?>">
<input type="hidden" name="courseTitle"
value="<?php echo $row['COURSE_TITLE']; ?>">
<input type="submit" value="Delete">
</form>
</td>
<td><?php echo $row['LASTNAME']; ?></td>
<td><?php echo $row['FIRSTNAME']; ?></td>
<td><?php echo $row['EMAIL_PHONE']; ?></td>
<td>$<?php echo $row['COURSE_TITLE']; ?></td>
<td><?php echo $row['SIGNUP_DATE']; ?></td>
</tr>
<?php } // end loop ?>

I say normally, because what you have is what I call a start/stop of PHP and HTML, dropping in and out of the PHP parser, and more difficult to maintain and debug. Although the output would be identical, this is a little easier to read and maintain.

<?php
$output=null; // to squelch concatenation warnings
$signup_url = '/course_registration/delete_course_signups.jsp';
// start loop
while ($row = mysql_fetch_array($result)) {
$output .= '
<tr>
<td>
<form action="$signup_url" method="post">
<input type="hidden" name="lastName"
value="' . $row['LASTNAME'] . '">
<input type="hidden" name="firstName"
value=' . $row['FIRSTNAME'] . '">
<input type="hidden" name="courseTitle"
value="' . $row['COURSE_TITLE'] . '">
<input type="submit" value="Delete">
</form>
</td>
<td>' . $row['LASTNAME'] . '</td>
<td>' . $row['FIRSTNAME'] . '</td>
<td>' . $row['EMAIL_PHONE'] . '</td>
<td>' . $row['COURSE_TITLE'] . '</td>
<td>' . $row['SIGNUP_DATE'] . '</td>
</tr>
} // end loop
echo "<table>$output</table>";
?>

I hope that comes close . . . an unrelated note, do this

/course_registration/delete_course_signups.jsp

and not this

../course_registration/delete_course_signups.jsp

The toothpick syndrome will eventually drive you mad. :-) Starting with the leading slash allows you to move a file anywhere in the system and still find the resource, whether it's a page, CSS, JS, or whatever. I've actually seen these

../../../images/omg_whereami.jpg
../../images/omg_whereami.jpg
images/omg_whereami.jpg

when this works for all three.

/images/omg_whereami.jpg

calmseas

9:17 pm on Aug 20, 2010 (gmt 0)

10+ Year Member



EXCELLENT!.............and thank you for your helpful suggestions.