Link to home
Start Free TrialLog in
Avatar of saul_roldan
saul_roldan

asked on

Problemas tratando de conectarme a la base de datos

Estoy trabajando en una pagina la cual esta conectada a la base de datos. Necesito traer una porcion de una de sus paginas. Cuando el visitante lee el resumen del articulo tiene una linea que dice: "para leer articulo completo presione aqui" este link necesito que se conecte a la base de datos para que traiga el articulo completo.

En la base de datos e creado una pagina que tiene "id", "articulo corto", y "articulo largo" no se si esta es la mejor manera de hacerlo. estoy trabajando en dreamweaver y este es el codigo que produce.

cuando el visitante presiona el link la version corta debe desaparecer y la larga tomar su lugar. ¿como podria hacer un If..else para lograr esto? Estoy usando PHP
<?php require_once('../Connections/newWeb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
$colname_rs_indexCont = "-1";
if (isset($_GET['pagkey1'])) {
  $colname_rs_indexCont = $_GET['pagkey1'];
}
mysql_select_db($database_newWeb, $newWeb);
$query_rs_indexCont = sprintf("SELECT id, `verision corta` FROM indexCont WHERE id = 0", GetSQLValueString($colname_rs_indexCont, "int"));
$rs_indexCont = mysql_query($query_rs_indexCont, $newWeb) or die(mysql_error());
$row_rs_indexCont = mysql_fetch_assoc($rs_indexCont);
$totalRows_rs_indexCont = mysql_num_rows($rs_indexCont);
 
mysql_select_db($database_newWeb, $newWeb);
$query_rs_indexNav = "SELECT id FROM indexCont ORDER BY id ASC";
$rs_indexNav = mysql_query($query_rs_indexNav, $newWeb) or die(mysql_error());
$row_rs_indexNav = mysql_fetch_assoc($rs_indexNav);
$totalRows_rs_indexNav = mysql_num_rows($rs_indexNav);
 
$colname_rs_IndexVerLarga = "-1";
if (isset($_GET['pagkey'])) {
  $colname_rs_IndexVerLarga = $_GET['pagkey'];
}
mysql_select_db($database_newWeb, $newWeb);
$query_rs_IndexVerLarga = sprintf("SELECT id, `version larga` FROM indexCont WHERE id = %s", GetSQLValueString($colname_rs_IndexVerLarga, "int"));
$rs_IndexVerLarga = mysql_query($query_rs_IndexVerLarga, $newWeb) or die(mysql_error());
$row_rs_IndexVerLarga = mysql_fetch_assoc($rs_IndexVerLarga);
$totalRows_rs_IndexVerLarga = mysql_num_rows($rs_IndexVerLarga);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
 
<body>
<p><a href="LinkContArticulo.php?pagkey=<?php echo $row_rs_indexNav['id']; ?>">abrir version larga</a></p>
<p><?php echo $row_rs_indexCont['verision corta']; ?><?php echo $row_rs_IndexVerLarga['version larga']; ?></p>
</body>
</html>
<?php
mysql_free_result($rs_indexCont);
 
mysql_free_result($rs_indexNav);
 
mysql_free_result($rs_IndexVerLarga);
?>

Open in new window

Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Hi saul_roldan,

Can we get a repost in English, please?  Nearly all of the Experts can help you in English.  Very few of us can help you in Spanish.
Jason.  I ran the code through Google Translator and the question seems to translate well....

--- Translation  ---------------------------------------------------------------------------------------------------
I am working on a site which is connected to the database. I need to bring a portion of one of its pages. When the visitor read the summary of the article takes a line that reads: "To read article in full Click Here" I need this link that connects to the database to bring the article in full.

In the database and created a page that has "id", "short article" and "long article" I do not know if this is the best way to do it. I'm working on dreamweaver and this is the code it produces.

when the visitor pressed the link the shortened version must disappear and eventually take their place. What could make a If .. else to achieve this? I am using PHP
--------------------------------------------------------------------------------------------------------------------

I will provide an answer in both languages for now (using Google again!), but please post in English if possible! :-)

Instead of having 2 database columns - 'long article' & 'short article', I would just have 'long article'.  You can get the database to select the first few characters from this column for your page.  PHP can also simply show the first, say, 200 characters from the column instead of the whole text.

You could then create a new page to get the whole article as required.

--- Spanish ------------------------------------------------------------------------------------------------------
En lugar de tener 2 bases de datos columnas - «largo artículo 'y' breve artículo, sólo quisiera tener a largo artículo». Puede obtener la base de datos para seleccionar los primeros caracteres de esta columna para su página. PHP también puede simplemente mostrar el primer lugar, decir, 200 caracteres de la columna en lugar de la totalidad del texto.

Usted podría crear una nueva página para obtener la totalidad del artículo, según sea necesario.
Hi Rouchie,

I could have done that...too tired to think.
Get some rest!  It's PHP so there's no danger of me solving it while you sleep... ;-)
Avatar of saul_roldan
saul_roldan

ASKER

Rouchie: can you explain in a simple way how can I do what you say.
On the page where you need to display the short versions, use this code to connect to the database:

SELECT
    id,
    LEFT([version larga], 100) & "..." AS [version larga]
FROM
    indexCont
ORDER BY
    id

You can see that LEFT(xxxx, 100) forces the database to only get the first 100 characters from that database field.  We then tell it to add "..." to the end of whatever it finds.
How you tell to add the rest os the text?

Im new doing php can you be so kind and show me how to creat a code for the link that will add the rest of the text in the same page

If you want to add the rest of the text to the same page then you need a different approach.

Because we need all the text, we need to get it all from the database:

  SELECT id, [version larga] FROM indexCont ORDER BY id

Then in PHP you can manipulate the text to only show the first part until a 'more' link is clicked.  I do not use PHP (I use ASP) so I do not know the code to do this.  The command you need however is the SUBSTR command (http://uk3.php.net/manual/en/function.substr.php)

Jason1178 does use PHP but he is in Los Angeles and will not be back for a few hours...

Thank for your help
SELECT id, [version larga] on "version larga" this will be the name of my page or the name of the colum?
Its the name of the database column
Jason1178 or anyone out there: Im using dreamweaber to create my php code. How can I ad a substr() to retrive the first paragraph of "versArticulo"

<?php echo $row_rs_Index['versArticulo']; ?>

thanks for your help
You can't get the first paragraph because the SUBSTR function works to a set number of characters.  Therefore you must get a fixed number of characters each time:

Try this and let me know if it grabs the first part of the database field:

<?php echo substr($row_rs_Index['versArticulo'], 0, 100) . "..."; ?>
It works this way <?php echo substr($row_rs_Index['versArticulo'],0,-100) . "..."; ?>


 now how to bring with a link the rest of the text
Okay first put this javascript in the <head> part of the page.  This allows us to show/hide the text:

<head>
<script type="text/javascript">
/* <![CDATA[ */
      function toggleVis(object)
            {
            if(document.getElementById)
                  {
                  var object = document.getElementById(object);
                  if(object.style.display=='none'){
                        object.style.display = '';
                  } else {
                        object.style.display = 'none';
                  }
            return false;
            }
      }
/* ]]> */
</script>
</head>


Now inside the <body> part, do this:


<p>
  <?php echo substr($row_rs_Index['versArticulo'],0,-100) . " <a href=\"#\" onclick=\"toggleVis('". $row_rs_Index['id'] ."')\">More</a>"; ?>
  <span id="<?php echo $row_rs_Index['id']; ?>" style="display:none;">
	<?php echo $row_rs_Index['versArticulo']; ?>
  </span>
</p>

Open in new window

is not working with this code. It show all the text and duplicate the content and put the word more at the end
Okay please try this...

<head>
<script type="text/javascript">
/* <![CDATA[ */
      function toggleVis(object, linkid)
            {
            if(document.getElementById)
                  {
              var mylink = document.getElementById(linkid);
              mylink.style.display = 'none';
                  var object = document.getElementById(object);
                  if(object.style.display=='none'){
                        object.style.display = '';
                  } else {
                        object.style.display = 'none';
                  }
            return false;
            }
      }
/* ]]> */
</script>
</head>

<p>
  <?php echo substr($row_rs_Index['versArticulo'],0,-100) ."<a href=\"#\" id=\"link".$row_rs_Index['id']."\" onclick=\"toggleVis('". $row_rs_Index['id'] .", link".$row_rs_Index['id']."')\"> More</a>"; ?>
  <span id="<?php echo $row_rs_Index['id']; ?>" style="display:none;">
        <?php echo substr($row_rs_Index['versArticulo'],-100); ?>
  </span>
</p>

Open in new window

I get the first paragraph as I want but the more link dont work
Can you post the HTML code from the page in the browser, so I can see how the PHP translates into HTML ?

<?php require_once('../Connections/newWeb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
$maxRows_rs_Index = 10;
$pageNum_rs_Index = 0;
if (isset($_GET['pageNum_rs_Index'])) {
  $pageNum_rs_Index = $_GET['pageNum_rs_Index'];
}
$startRow_rs_Index = $pageNum_rs_Index * $maxRows_rs_Index;
 
mysql_select_db($database_newWeb, $newWeb);
$query_rs_Index = "SELECT id, versArticulo FROM indexCont ORDER BY id=0 ASC";
$query_limit_rs_Index = sprintf("%s LIMIT %d, %d", $query_rs_Index, $startRow_rs_Index, $maxRows_rs_Index);
$rs_Index = mysql_query($query_limit_rs_Index, $newWeb) or die(mysql_error());
$row_rs_Index = mysql_fetch_assoc($rs_Index);
 
if (isset($_GET['totalRows_rs_Index'])) {
  $totalRows_rs_Index = $_GET['totalRows_rs_Index'];
} else {
  $all_rs_Index = mysql_query($query_rs_Index);
  $totalRows_rs_Index = mysql_num_rows($all_rs_Index);
}
$totalPages_rs_Index = ceil($totalRows_rs_Index/$maxRows_rs_Index)-1;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
 
 
<script type="text/javascript">
/* <![CDATA[ */
      function toggleVis(object, linkid)
            {
            if(document.getElementById)
                  {
              var mylink = document.getElementById(linkid);
              mylink.style.display = 'none';
                  var object = document.getElementById(object);
                  if(object.style.display=='none'){
                        object.style.display = '';
                  } else {
                        object.style.display = 'none';
                  }
            return false;
            }
      }
/* ]]> */
</script>
 
 
</head>
 
<p>
  <?php echo substr($row_rs_Index['versArticulo'],0,-581) ."<a href=\"#\" id=\"link".$row_rs_Index['id']."\" onclick=\"toggleVis('". $row_rs_Index['id'] .", link".$row_rs_Index['id']."')\"> More</a>"; ?>
  <span id="<?php echo $row_rs_Index['id']; ?>" style="display:none;">
        <?php echo substr($row_rs_Index['versArticulo'],-581); ?>
  </span>
</p>
</body>
</html>
<?php
mysql_free_result($rs_Index);
?>

Open in new window

I need the code from the browser please, not Dreamweaver!
Im new in this are you talking about source code in the browser?
Yes

So we can see how the PHP is writing the HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


<script type="text/javascript">
/* <![CDATA[ */
      function toggleVis(object, linkid)
            {
            if(document.getElementById)
                  {
              var mylink = document.getElementById(linkid);
              mylink.style.display = 'none';
                  var object = document.getElementById(object);
                  if(object.style.display=='none'){
                        object.style.display = '';
                  } else {
                        object.style.display = 'none';
                  }
            return false;
            }
      }
/* ]]> */
</script>


</head>

<p>
  <p span class="titulo">El Infierno y la Redenciýn</p>

<p class="autor">Saýl Roldýn</p>

 <p><img src="../../images/juicioFinal.jpg" width="100" height="100" class="img" />La doctrina del infierno es el estudio del destino final de los perdidos y la erradicaciýn de la maldad en el mundo.&nbsp; Pertenece al ýrea&nbsp; de la escatologýa que estudia los eventos que ocurrirýn en los ýltimos dýas.&nbsp; En este estudio deseamos entrar en dialogo con aquellos que sostienen esta doctrina. Muchos honestos cristianos difieren en la interpretaciýn del castigo final, y aunque es importante el entender lo que las escrituras enseýan tocante a este tema, no determina nuestra salvaciýn final. </p>
  <p><a href="#" id="link1" onclick="toggleVis('1, link1')"> More</a>  <span id="1" style="display:none;">

        Uno de los problemas que muchos encuentran en la doctrina del infierno es que ella requiere la incorruptibilidad del cuerpo o inmortalidad de toda la persona humana. Un castigo eterno requiere un cuerpo inmortal ya que Jesýs enseýo que todos los hombres resucitarýn para enfrentar el juicio y experimentar el castigo que les corresponda.&nbsp; La Biblia no habla del castigo del alma como una entidad aparte del cuerpo, al hombre se le percibe como una unidad, el hombre total es el que experimentarý el juicio, por lo que se hace necesaria la doctrina de la resurrecciýn. </p>
 
  </span>
</p>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Rouchie
Rouchie
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works! Thank you for your effort.

Just for my information: can this be done only with php? Do I need Javascript to do this kind of linking
It only really needs javascript.  The code in the <script> tags is used to show the hidden text.

We use PHP here just to split the text into 2 parts, and then we hide the second part.
I have two articule in the same page that will use this code do I need to do anything to this code

<?php echo substr($row_rs_Index['versArticulo'],0,-581)

make it work for each of them:
The number -581 will not work because that is specific to the text we used previously.

Because I don't write PHP code I do not know how to use SUBSTR, but here is how we need it to work:

  1.  In the short text, use the first 100 characters
  2.  In the long text, use all the text except the first 100 characters

From reading the manual on the PHP web site, to use 100 characters each time, it should be:


<p>
  <?php echo substr($row_rs_Index['versArticulo'], 0, 100) ."<a href=\"#\" id=\"link".$row_rs_Index['id']."\" onclick=\"toggleVis('". $row_rs_Index['id'] ."', 'link".$row_rs_Index['id']."')\"> More</a>"; ?>
  <span id="<?php echo $row_rs_Index['id']; ?>" style="display:none;">
        <?php echo substr($row_rs_Index['versArticulo'],101); ?>
  </span>
</p>

Open in new window

Wow, Rouchie got a PHP solution.  That's what I get for going to sleep.

See, the magic was in you all along...
Wow this PHP thing is really awesome.  I  think I might format my machine and rebuild as LAMP....

Actually perhaps not, but reading through the PHP manual made me realise that it's pretty much the same as ASP, just with slightly different syntax.  Still, I've leave the PHP questions in your capable hands, if that's okay...!  ;-)