Jump to content

[SOLVED] interesting... create your own tag?


optikalefx

Recommended Posts

i have a text box that saves to a database.  www.4tenproductions.com/jrpass.php

i also have pictures on a server, that each have a unique number. www.4tenproductions.com/ir/uploadedfiles/picture1.png

then there is a website that grabs all the info and uses it www.robbertlaw.com

 

i want to be able to type in the text box  <include picture# 15>  Or something easy like that

and then when i load robbertlaw.com it translates it into this:

<?

$con = mysql_connect("xxxx","xxxx","xxxx");

mysql_select_db("update1", $con);

$sql = mysql_query("SELECT pic FROM pics WHERE num ='15'");

while($row = mysql_fetch_array($sql))

{

echo "<img src='http://www.4tenproductions.com/ir/uploadedfiles2/" . $row['pic'] . "'></td></tr>";

}

?>

 

 

but i also want to make sure if u go back to the text box, it shows up as <include picture: 15>

and still when you go to robbertlaw.com it uses that code to actually generate the picture.

 

is this possible at all?

 

just a note, when you submit the textbox to the database this is the code:

$myText = mysql_real_escape_string($_POST["teampage"]);

 

so its escaping, and the reason for that is so that you can use single quotes without messing the code up.

Link to comment
Share on other sites

What you'll want to do is create a function call getImages which parses the text in $myText to translate <include picture# 15> into the appropriate image.

 

<?php

$con = mysql_connect("xxxx", "xxxx", "xxxx");
mysql_select_db("update1", $con);


// this function gets called which returns the appripitate image
function getImage($img)
{
    $query = "SELECT pic FROM pics WHERE num ='$img'";

    $result = mysql_query($query);

    // check there was one result from the query
    if(mysql_num_rows($result) == 1)
    {
        // get the result
        $row = mysql_fetch_assoc($result);

        // return the html for the html
        return '<img src="http://www.4tenproductions.com/ir/uploadedfiles2/' . $row['pic'] . '">';
    }
    else
    {
        // image not found in database
        return '[img#' . $img' Not found]';
    }
}

// dummy text
$txt = 'Hello world! <include picture# 1>

Welcome to my site! <include picture# 6>';

// use regex to replace <include picture# 123> with the appropiate image
// this will call the getImage function.
$new_txt = preg_replace('/<include picture# ([0-9]+)>/ise', "getImage($1)", $txt);

echo $txt . '<hr />' . $new_txt;

?>

You're best of running this code when you get the page data out of the database.

Link to comment
Share on other sites

I had a slight typo. Try this:

<?php

$con = mysql_connect("xxxx", "xxxx", "xxxx");
mysql_select_db("update1", $con);


// this function gets called which returns the appripitate image
function getImage($img)
{
    $query = "SELECT pic FROM pics WHERE num ='$img'";

    $result = mysql_query($query);

    // check there was one result from the query
    if(mysql_num_rows($result) == 1)
    {
        // get the result
        $row = mysql_fetch_assoc($result);

        // return the html for the html
        return '<img src="http://www.4tenproductions.com/ir/uploadedfiles2/' . $row['pic'] . '">';
    }
    else
    {
        // image not found in database
        return '[img#' . $img . ' Not found]';
    }
}

// dummy text
$txt = 'Hello world! <include picture# 1>

Welcome to my site! <include picture# 6>';

// use regex to replace <include picture# 123> with the appropiate image
// this will call the getImage function.
$new_txt = preg_replace('/<include picture# ([0-9]+)>/ise', "getImage($1)", $txt);

echo $txt . '<hr />' . $new_txt;

?>

Link to comment
Share on other sites

I really wished that worked, i see what you changed, but to no avail.

 

can i email you and trust you with the database login info to test it out and see if it works?

 

Ill keep going thru and see whats wrong but, let me know if we can talk that way.

 

my email is sean@4tenonline.com

Link to comment
Share on other sites

Iv tried everything i can think of

i made your code over again, changing the database stuff to the exact same codes that i tested and worked

i basically connected, ran a query and echoed the picture, and that worked great. so heres the new code

 

that still shows up not working...

<?php

 $con = mysql_connect("xxxx","xxxx","xxxx"); 
mysql_select_db("update1", $con);


// this function gets called which returns the appripitate image
function getImage($img)
{
  
$sql = mysql_query("SELECT pic FROM pics WHERE num='$img'");

   while($row = mysql_fetch_array($sql))
{
$url1 = "<img src='http://www.4tenproductions.com/ir/uploadedfiles2/" . $row['pic'] . "'>";
}

        // return the html for the html
        return $url1;
   
}

// dummy text
$txt = 'Hello world! <include picture# 1>

Welcome to my site! <include picture# 6>';

// use regex to replace <include picture# 123> with the appropiate image
// this will call the getImage function.
$new_txt = preg_replace('/<include picture# ([0-9]+)>/ise', "getImage($1)", $txt);

echo $txt . '<hr />' . $new_txt;

?>

 

Link to comment
Share on other sites

the problem is that the page is blank, and it should be showing text then a picture, then text, then another picture.

 

i updated with a diff code, and basically a new solution/problem, but smaller in a new thread.

 

i used what i learned here, and im close, i just need to figure out one last thing, so i i figure it was a different topic

Link to comment
Share on other sites

My code should be working fine. I even tested it before I posted it and it worked no probs.

 

Do you have access to your php.ini? If you do turn a setting called display_errors on and make sure error_reporting is set to E_ALL. Save the php.ini and then restart your server (Apache, IIS etc). re run your script.

 

if you get any errors post them here in full.

 

I have now added in a status messages. if the script is failing a specifc place you'll now know where it is failing:

<?php

echo 'Connecting to MySQL...<br />';

$con = mysql_connect('localhost', 'root');
mysql_select_db('test', $con);

echo 'Connection established....<br /><br />';

$getImageLog = null;

// this function gets called which returns the appripitate image
function getImage($img)
{
    global $getImageLog;

    $getImageLog .= 'Defining $query';

    $query = "SELECT pic FROM pics WHERE num ='$img'";

    $getImageLog .= ' - (' . $query . ') - Running query...<br />';

    $result = mysql_query($query);

    $getImageLog .= 'Query has ran with no errors<br />Getting the image<br />';

    // check there was one result from the query
    if(mysql_num_rows($result) == 1)
    {
        // get the result
        $row = mysql_fetch_assoc($result);

        $getImageLog .= 'Image recieved (' . $row['pic'] . ' for img#' . $img . ')<br /><br />';

        // return the html for the html
        return '<img src="http://www.4tenproductions.com/ir/uploadedfiles2/' . $row['pic'] . '">';
    }
    else
    {
        $getImageLog .= 'img#' . $img . ' not found<br /><br />';

        // image not found in database
        return '[img#' . $img . ' Not found]';
    }
}

echo 'Defining $txt variable...<br />';

// dummy text
$txt = 'Hello world! <include picture# 1>

Welcome to my site! <include picture# 6>';

echo '$txt variable defined. Now to parse $txt...<br />';

// use regex to replace <include picture# 123> with the appropiate image
// this will call the getImage function.
$new_txt = preg_replace('/<include picture# ([0-9]+)>/ise', "getImage($1)", $txt);

echo 'getImage() log:<div style="padding:20px;">' . $getImageLog . '</div><br />Result....<br /><br />';

echo htmlentities($txt) . '<hr />' . $new_txt;

?>

Link to comment
Share on other sites

I'm using my own database. I'm not using yours. I have a havea table setup like so just to test my script

+------+--------------+

|  num  |    pic          |

+------+--------------+

|    1    | 4log.png      |

|    3    | Picture 4.png |

+-------+-------------+

 

The images show because I am using your websites url.

 

my source code

Link to comment
Share on other sites

now im clueless.  if yours works and mine doesnt...

 

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /hermes/web04/b1609/pow.4ten/htdocs/testing.php on line 3

 

thats what im getting.

 

and my sql info cant be wrong because i copy and pasted from a working script.

 

any ideas what could be causing this?

Link to comment
Share on other sites

Looks like your code gets corrupt when you upload it to your site and is causing that error.

 

Make sure when you save the PHP file the file encoding is set to ANSI. Most Text editors should save in this format. Other encodings such as UTF-8 can cause problems.

 

Download the attached file edit it and then upload.

 

[attachment deleted by admin]

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.