Jump to content

Display database rows in html by id


Matic

Recommended Posts

Hey, I want to list all of my database entries to a nice html format. For every unique id there is one row displayed out from my database to my php/html. I just don't know how to do that. I tried using while loop and foreach I just don't know how to properly structure them... My query and php code looks like this:

$query = mysql_query("SELECT * FROM sport");

while($row = mysql_fetch_array($query))
{
    $result = $row['sport_ime'].' '.$row['sezona'];
}
Link to comment
Share on other sites

$query = mysql_query("SELECT * FROM sport");

while($row = mysql_fetch_array($query))
{
   echo $row['sport_ime'].' '.$row['sezona'].'<br>';
}

 

:D no no, echo row just lists entire database in one row like so: "foo1988bar2000etc..." I want each new id in a new row ... I know there is something with for each() statement I just can't format it right

Link to comment
Share on other sites

Did you even try what I posted, I have a line break after each row so they are all on new lines.  You will need to be a bit more specific as to how you want it formatted than simply saying you want it on a new row, cause that is what I gave you.  Do you want it in a new table row or simply a new line on the screen, what?

Link to comment
Share on other sites

Did you even try what I posted, I have a line break after each row so they are all on new lines.  You will need to be a bit more specific as to how you want it formatted than simply saying you want it on a new row, cause that is what I gave you.  Do you want it in a new table row or simply a new line on the screen, what?

Oh damn I didn't see that break tag there, my bad! Would you also be so kind to post a solution, if you know one, how to make every row clickable so that user can access entries and modify/delete them?

 

Also does anyone know how to store that line

echo $row['sport_ime'].' '.$row['sezona'].'<br>';

inside $_SESSION variable correctly?

Edited by Matic
Link to comment
Share on other sites

 

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') { //if page is posted, then form was submitted.
 $update = sprintf("UPDATE sport SET sport_ime='%s', sezona='%s' WHERE id=%d",
                              mysql_real_escape_string($_POST['sport_ime']),
                              mysql_real_escape_string($_POST['sezona']),
                              $_GET['id']); //put values into proper formatted, and escaped string.
 if(!mysql_query($update)) { //either query successfully.
   echo mysql_error(); //or error out.
 }
}
$sql = "SELECT * FROM sport"; //build the main query.
if(!emtpy($_GET['id']) { //if the $_GET index is present, then we need to get just that record.
$edit = 1; //specify this page is an edit.
 $id = (int)$_GET['id']; //cast value to integer.
 $sql .= "WHERE id = $id"; //add the where clause to the query.
}
$query = mysql_query($sql); //run the query.

while($row = mysql_fetch_array($query)) //fetch results.
{
if(isset($edit)) { //if this page is an edit page.
 echo '<form action="" method="post">' . "\n"
       .'<input type="hidden" name="id" value="' . $row['id'] . '" \>' . "\n"
        .'<label for="sport_ime">Sport IME</label>' . "\n"
        .'<input type="text" name="sport_ime" value="' . $row['sport_ime'] .'"/>' . "\n"
        .'<label for="sezona">Sezona</label>' . "\n"
       .'<input type="text" name="sezona" value="' . $row['sezona'] . '">' . "\n"
      .'<input type="submit" name="submit" value=" SUBMIT " />' . "\n"
      .'</form>'; //build the form.
 } else { //if it isn't.
  echo $row['sport_ime'].' '.$row['sezona'].'<a href="?id=' . $row['id'] . '">Edit</a><br>'; //show the rows with a clickable edit link (which reloads the page and starts over).
 }
}
Link to comment
Share on other sites

Oh damn I didn't see that break tag there, my bad! Would you also be so kind to post a solution, if you know one, how to make every row clickable so that user can access entries and modify/delete them?

 

Also does anyone know how to store that line

echo $row['sport_ime'].' '.$row['sezona'].'<br>';

inside $_SESSION variable correctly?

 

 

Fetch all rows and push it inside an associative array.

 

Try,

$query = mysql_query("SELECT * FROM sport");

$_SESSION = array();

while($row = mysql_fetch_array($query))
{
   $_SESSION[] = array($row['sport_ime'],$row['sezona']); 
}

// dump the data

echo '<pre>'.print_r($_SESSION, true).'</pre>';

Or....for best performance use mysql_fetch_assoc() function.

while($row = mysql_fetch_assoc($query))
{
   $_SESSION[] = array($row['sport_ime'],$row['sezona']); 
}
Edited by jazzman1
Link to comment
Share on other sites

 

Fetch all rows and push it inside an associative array.

 

Try,

$query = mysql_query("SELECT * FROM sport");

$_SESSION = array();

while($row = mysql_fetch_array($query))
{
   $_SESSION[] = array($row['sport_ime'],$row['sezona']); 
}

// dump the data

echo '<pre>'.print_r($_SESSION, true).'</pre>';

Or....for best performance use mysql_fetch_assoc() function.

while($row = mysql_fetch_assoc($query))
{
   $_SESSION[] = array($row['sport_ime'],$row['sezona']); 
}

 

That is all fine and dandy but if I call this session in another file and try to echo it, it still displays only the last element of the array...

Link to comment
Share on other sites

 

How did you echo it? Can I see the code?

Also show us the result of:

 

  • // dump the data
  • echo '<pre>'.print_r($_SESSION, true).'</pre>'; 

 

Well yes I echo in my html file like so:

<?php echo($_SESSION['result']); ?>

in my backend php I do the session storage like so:

while($row = mysql_fetch_array($query))
{
   $_SESSION['result'] = $row['sport_ime'].' '.$row['sezona'].'<br>';
}

// and then redirect back to html where it should echo the stuff
        
header('Location:../view/vnos.php',true,303);
exit;

The results you requested were just array => 0 and 1 for the last element in my array

Link to comment
Share on other sites

No, you're using sessions. Go to php.net and check out how to..... 

 

Try next:

 

main.php

<?php

session_start();

$query = mysql_query("SELECT * FROM sport") or die(mysql_error());

$outputs = array();

while($row = mysql_fetch_array($query)) {
    
    $outputs[] = array($row['sport_ime'],$row['sezona']);
    
}

$_SESSION['output']=$outputs;

// and then redirect back to html where it should echo the stuff   
header('Location:../view/vnos.php',true,303);
exit;

vnos.php

<?php

session_start();

echo '<pre>'.print_r($_SESSION['output'], true).'</pre>';

You should be consider how to use a session_start() and how to destroy that session.

 

Go to php.net and come back again!

 

EDIT: session_start() should be on very top of the page!

 

I've made a typo -> $outputs[] = array($row['sport_ime'],$row['sezona']);

Edited by jazzman1
Link to comment
Share on other sites

Something like this .

  echo "<table align=\"center\" width=\"500\" border=\"0\" background=\"images/FormBG.png\">";
  echo "<tr><td colspan=\"5\" height=\"50\"><font size=\"5\">Excision Type</font><td></tr>";
  echo "<tr><th>ID</th><th>Type</th><th>Name</th></tr>";
  
  while ($stmt->fetch())
  { 
     echo "<form method=\"post\" action=\"excision.php\"> ";
     echo "<tr><td><input name=\"excision_id\" type=\"hidden\" value=" . $excision_id . ">" . $excision_id . "</td>" ;
	 echo     "<td><input name=\"excision_type\" type=\"text\" value=" . $excision_type . "></td>";
	 echo     "<td><input name=\"excision_name\" type=\"text\" value=" . $excision_name . "></td>";
	 echo     "<td><input name=\"edit\" src=\"/IC/images/Clipboard 3.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Edit\"></td>";
	 echo     "<td><input name=\"delete\" src=\"/IC/images/Close - Cancel 1.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Delete\"></td></tr>";
	 echo "</form>";
  }
  echo "<form method=\"post\" action=\"excision.php\"> ";
  echo "<tr><td> </td>" ;
  echo "    <td><input name=\"a_excision_type\" type=\"text\"></td>";
  echo "    <td><input name=\"a_excision_name\" type=\"text\"></td>"; 
  echo "    <td><input name=\"add\" src=\"/IC/images/Add Green.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Add\"></td>";
  echo "    <td></td></tr>";
  echo "</form>" ;
  echo "</table>" ;
Link to comment
Share on other sites

Stickler for Syntax maybe , but no Microsoft fan. Unix anyday of the week.

I just found this gottcha with one of the lines where I had a value with two words with a space between.

Saved to the database ok , but when it redrew the table it only showed the first word. Fixed it with this tricky "\" combo.

 

Before

echo     "<td><input name=\"excision_type\" type=\"text\" value=" . $excision_type . "></td>";

After

echo     "<td><input name=\"excision_type\" type=\"text\" value=\"" . $excision_type . "\"></td>";

Shows you the importance of those double quotes. lol

Link to comment
Share on other sites

Thanks for all the help but my session variable still shows only the last element of my row. When it is echoes directly it is okay but when it is stored in session it only displays the last element of array...

 

I make my session variable store array like so:

$outputs = array();
while($row = mysql_fetch_array($query))
{

    $_SESSION['row_id'] = $row['id'];
    $outputs = $row['sport_ime'].' '.$row['sezona'].'<a href=../view/modifikacije.php> Edit</a><br>';
    $_SESSION['output'] = $outputs;

}

and then on another page I call that variable like so:

$outputs = $_SESSION['output'];
echo  ($outputs);

It just echoes one row! It is the last entry in my database, why doesn't it want to echo everything? People at another site say that I am repeatedly overwriting the same variables! But I don't know how to avoid that

Edited by Matic
Link to comment
Share on other sites

It's cause you are reassigning the value of the output with every loop.

$_SESSION['output'][] = $outputs; // Need the [] so that it adds a new array element rather than over-writing the previous output.

Then you would need to either call a specific array key or use a foreach loop on the $_SESSION['output'] to echo out the outputs.

Link to comment
Share on other sites

It's cause you are reassigning the value of the output with every loop.

$_SESSION['output'][] = $outputs; // Need the [] so that it adds a new array element rather than over-writing the previous output.

Then you would need to either call a specific array key or use a foreach loop on the $_SESSION['output'] to echo out the outputs.

yes this! Now how do I do this properly with foreach statement? This is what I am asking this whole time, though other answers in this thread helped me greatly also

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.